I am debugging a feature. And I found two different query results of the same sql statement. The sql statement executed against the same database and table.
SQL execution by Yii2's ActiveQuery gave result #1 (8 records). SQL execution by Navicata gave result #2 (11 records).
Here is the code:
"count($payload["id_tag"])" is always zero
$where = [];
$where[] = "and";
$where[] = "is_delete = 0";
$where[] = "linked_id_question <> -1";
$where[] = ["in", "status", [-3, -2, 1, 4]];
$where[] = ["in", "id_kategori", $payload["id_kategori"]];
if( count($payload["id_tag"]) > 0 )
{
$where[] = ["in", "ftt.id_tag", $payload["id_tag"]];
}
$test = ForumThread::find()
->join("join", "forum_thread_tag ftt", "ftt.id_thread = forum_thread.id")
->where($where)
->orderBy("time_create desc")
->all();
$total_rows = count($test);
Here is the sql statement generated by Yii's activequery:
SELECT
`forum_thread`.*
FROM
`forum_thread`
join `forum_thread_tag` `ftt` ON ftt.id_thread = forum_thread.id
WHERE
(is_delete = 0) AND
(linked_id_question <> -1) AND
(`status` IN (-3, -2, 1, 4)) AND
(`id_kategori`=46)
ORDER BY `time_create` DESC;
Here is the sql statement I wrote on the Navicat:
select
*
from
forum_thread
join forum_thread_tag ftt on ftt.id_thread = forum_thread.id
where
is_delete = 0 and
linked_id_question <> -1 and
status in (-3, -2, 1, 4) and
id_kategori in (46)
order by
time_create desc;
Both queries were executed against the same database and table. No changes on the records.
As you can see, the sql statement joins forum_thread and forum_thread_tag tables (one-to-many relationship) and it SHOULD return 11 records. But why Yii2's activequery returned 8 records (as if it process the result with DISTINCT operator).
Any idea why the Yii's ActiveQuery gave different result?