Answer the question
In order to leave comments, you need to log in
Yii2 SQL Query Builder. Same query but different fetch results?
Colleagues help out PLYYYZZ!
For two weeks now I can not defeat one oddity in Yii2.
In general, I create a query through the Yii2 SQL query builder (I mean, I build a SQL query using Yii2 methods, not createCommand)
I create the following query:
$obj = Customers::find()
->select(['customers.*', 'ps.price'])
->join('LEFT JOIN', 'product ps', 'ps.customer_id = customers.id')
->orderBy(['ps.price' => SORT_DESC])
->offset(1)->limit(6)
->asArray();
$obj->all();
0 => Array (id=>3, name=>user3, price=>43000),
1 => Array (id=>4, name=>user4, price=>42099),
2 => Array (id=>11, name=>user11, price=>25783),
3 => Array (id=>7, name=>user7, price=>25783)
$obj->createCommand()->sql;
SELECT `customers`.*, `ps`.`price` FROM `customers` LEFT JOIN `product` `ps` ON ps.customer_id = customers.id ORDER BY `ps`.`price` DESC LIMIT 6 OFFSET 2
$pns_command = Yii::$app->db->createCommand('
SELECT `customers`.*, `ps`.`price` FROM `customers` LEFT JOIN `product` `ps` ON ps.customer_id = customers.id ORDER BY `ps`.`price` DESC LIMIT 6 OFFSET 1
');
try {
$pns = $pns_command->queryAll();
} catch (\Exception $e) {
d::pe($e->getMessage());
}
0 => Array (id=>7, name=>user7, price=>43087),
1 => Array (id=>3, name=>user3, price=>43000),
2 => Array (id=>4, name=>user4, price=>42099),
3 => Array (id=>4, name=>user4, price=>32999),
4 => Array (id=>3, name=>user3, price=>31000),
5 => Array (d=>11, name=>user11, price=>25783)
0 => Array (id=>3, name=>user3, price=>43000),
1 => Array (id=>4, name=>user4, price=>42099),
2 => Array (id=>11, name=>user11, price=>25783),
3 => Array (id=>7, name=>user7, price=>25783)
0 => Array (id=>7, name=>user7, price=>43087),
1 => Array (id=>3, name=>user3, price=>43000),
2 => Array (id=>4, name=>user4, price=>42099),
3 => Array (id=>4, name=>user4, price=>32999),
4 => Array (id=>3, name=>user3, price=>31000),
5 => Array (d=>11, name=>user11, price=>25783)
->offset(1)->limit(6)
0 => Array (id=>11, name=>user11, price=>43087,
1 => Array (id=>7, name=>user7, price=>43087,
2 => Array (id=>3, name=>user3, price=>43000,
3 => Array (id=>4, name=>user4, price=>42099,
4 => Array (id=>9, name=>user9, price=>22999,
5 => Array (id=>5, name=>user5, price=>22999,
6 => Array (id=>2, name=>user2, price=>9001,
7 => Array (id=>10, name=>user10, price=>7000,
8 => Array (id=>6, name=>user6, price=>7000,
9 => Array (id=>12, name=>user12, price=>6000,
10 => Array (id=>8, name=>user8, price=>6000,
11 => Array (id=>1, name=>user1, price=>3401,
0 => Array (id=>11, name=>user11, price=>43087,
1 => Array (id=>7, name=>user7, price=>43087,
2 => Array (id=>3, name=>user3, price=>43000,
3 => Array (id=>4, name=>user4, price=>42099,
4 => Array (id=>4, name=>user4, price=>32999,
5 => Array (id=>3, name=>user3, price=>31000,
6 => Array (id=>11, name=>user11, price=>25783,
7 => Array (id=>7, name=>user7, price=>25783,
8 => Array (id=>11, name=>user11, price=>23000,
9 => Array (id=>7, name=>user7, price=>23000,
10 => Array (id=>9, name=>user9, price=>22999,
11 => Array (id=>5, name=>user5, price=>22999,
12 => Array (id=>4, name=>user4, price=>21008,
13 => Array (id=>11, name=>user11, price=>21000,
14 => Array (id=>7, name=>user7, price=>21000,
15 => Array (id=>3, name=>user3, price=>21000,
16 => Array (id=>4, name=>user4, price=>19000,
17 => Array (id=>4, name=>user4, price=>15000,
18 => Array (id=>9, name=>user9, price=>12888,
19 => Array (id=>5, name=>user5, price=>12888,
20 => Array (id=>2, name=>user2, price=>9001,
21 => Array (id=>3, name=>user3, price=>8329,
22 => Array (id=>10, name=>user10, price=>7000,
23 => Array (id=>6, name=>user6, price=>7000,
24 => Array (id=>10, name=>user10, price=>6000,
25 => Array (id=>6, name=>user6, price=>6000,
26 => Array (id=>12, name=>user12, price=>6000,
27 => Array (id=>8, name=>user8, price=>6000,
28 => Array (id=>9, name=>user9, price=>5912,
29 => Array (id=>5, name=>user5, price=>5912,
30 => Array (id=>10, name=>user10, price=>4500,
31 => Array (id=>6, name=>user6, price=>4500,
32 => Array (id=>10, name=>user10, price=>4399,
33 => Array (id=>6, name=>user6, price=>4399,
34 => Array (id=>11, name=>user11, price=>3900,
35 => Array (id=>6, name=>user6, price=>3900,
36 => Array (id=>1, name=>user1, price=>3401,
37 => Array (id=>12, name=>user12, price=>2358,
38 => Array (id=>7, name=>user7, price=>2358,
39 => Array (id=>3, name=>user3, price=>2000,
40 => Array (id=>1, name=>user1, price=>1500,
41 => Array (id=>12, name=>user12, price=>1388,
42 => Array (id=>8, name=>user8, price=>1388,
43 => Array (id=>1, name=>user1, price=>1200,
44 => Array (id=>12, name=>user12, price=>1200,
45 => Array (id=>8, name=>user8, price=>1200,
46 => Array (id=>2, name=>user2, price=>900,
47 => Array (id=>1, name=>user1, price=>800,
48 => Array (id=>12, name=>user12, price=>650,
49 => Array (id=>8, name=>user8, price=>650,
50 => Array (id=>12, name=>user12, price=>500,
51 => Array (id=>8, name=>user8, price=>500,
52 => Array (id=>9, name=>user9, price=>399,
53 => Array (id=>5, name=>user5, price=>399,
54 => Array (id=>2, name=>user2, price=>330,
55 => Array (id=>1, name=>user1, price=>300,
56 => Array (id=>2, name=>user2, price=>290,
57 => Array (id=>2, name=>user2, price=>251,
58 => Array (id=>10, name=>user10, price=>200,
59 => Array (id=>5, name=>user5, price=>200,
Answer the question
In order to leave comments, you need to log in
The answer came to my mind.
In general, the decision was all 2.5 weeks before my eyes.
In general, when the Yii2 Query Builder generated a select object with data from a linked table,
several rows were selected from the linked table and attached to each row of the main table.
those. for example, 3 rows from a linked table were attached to the first row of the main table, for example, 7 rows from a linked table were attached to the second row of the main table, and so on.
A simple query returned a selection with duplicate main table IDs, like this:
0 => Array (id=>7, name=>user7, price=>43087),
1 => Array (id=>3, name=>user3, price=>43000),
2 => Array (id=>4, name=>user4, price=>42099),
3 => Array (id=>4, name=>user4, price=>32999),
4 => Array (id=>3, name=>user3, price=>31000),
5 => Array (d=>11, name=>user11, price=>25783)
0 => Array (id=>3, name=>user3, price=>43000),
1 => Array (id=>4, name=>user4, price=>42099),
2 => Array (id=>11, name=>user11, price=>25783),
3 => Array (id=>7, name=>user7, price=>25783)
// Строка основной таблицы
0 => Array (id=>7, name=>user7),
// Строки связной таблицы
Array( price=>43087 ),
// Строка основной таблицы
1 => Array (id=>3, name=>user3),
// Строки связной таблицы
Array(
price=>43000
price=>31000
),
// Строка основной таблицы
2 => Array (id=>4, name=>user4, price=>42099),
// Строки связной таблицы
Array(
price=>42099
price=>32999
),
// Строка основной таблицы
3 => Array (d=>11, name=>user11),
// Строки связной таблицы
Array( price=>25783 ),
0 => Array (id=>7, name=>user7, price=>43087),
1 => Array (id=>3, name=>user3, price=>43000),
2 => Array (id=>4, name=>user4, price=>42099),
3 => Array (id=>4, name=>user4, price=>32999),
4 => Array (id=>3, name=>user3, price=>31000),
5 => Array (d=>11, name=>user11, price=>25783)
$obj = Customers::find()
->select(['customers.*', 'products.price'])
->distinct()
->joinWith('product')
->orderBy(['products.price' => SORT_DESC])
->offset(1)->limit(6)
->asArray();
$obj->all();
0 => Array (id=>7, name=>user7, price=>43120),
1 => Array (id=>3, name=>user3, price=>43100),
2 => Array (id=>9, name=>user9, price=>42099),
3 => Array (id=>4, name=>user4, price=>32999),
4 => Array (id=>15, name=>user15, price=>31000),
5 => Array (d=>11, name=>user11, price=>25783)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question