A
A
akula222016-03-14 11:53:37
MySQL
akula22, 2016-03-14 11:53:37

Sql Query with temporary table in ActiveDataProvider?

I need to insert such a Sql query into ActiveDataProvider, tell me how?

SELECT * FROM ( 
    SELECT id, username, text, sender_id 
    FROM pm 
    WHERE user_id =2 AND sender_id !=2 AND STATUS !=2 
    ORDER BY created_at DESC
) AS tmp_table GROUP BY sender_id

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IIISpikerIII, 2016-03-14
@IIISpikerIII

How big is the need for a temporary table in such a query, but if there really is such a need, then you can insert it into the CSqlDataProvider

A
akula22, 2016-03-14
@akula22

Here is a request

$subQuery = (new Query())->select('id, username, text, sender_id')->from('pm')->where('user_id =2 AND sender_id !=2 AND STATUS !=2 ORDER BY created_at DESC');
    $query = self::find();
    $query->from(['AS tmp_table GROUP BY sender_id' => $subQuery]);
    $query->with(['profile']);

forms SELECT * FROM (SELECT `id`, `username`, `text`, `sender_id` FROM `pm` WHERE user_id =2 AND sender_id !=2 AND STATUS !=2 ORDER BY created_at DESC) `AS tmp_table GROUP BY sender_id ` LIMIT 10
looks like the same query that is needed, but for some reason it does not group, but if you manually remove the quotes from `AS tmp_table GROUP BY sender_id`, i.e. change to AS tmp_table GROUP BY sender_id then everything displays as it should, but how to force to generate request without these quotes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question