ORDER JOIN Left 未生效
ORDER JOIN Left not taking effect
我一定是做错了什么,因为我已经尝试了此处搜索中出现的所有建议。
我正在尝试加入 table 1 和 table 2,但是从 table 2 我只想 select 1 条目 匹配 table 上的 id 1。但我不希望它从 table 2 中选择任何条目,它需要是最新日期。
我有
$query->join('LEFT', '#table_2 AS o ON a.id = o.item_id');
我试过了
$query->join('LEFT', '#table_2 AS o ON a.id = o.item_id')->order('o.date', 'DESC');
重新排序所有返回的结果并且对连接结果没有影响
我也试过了
$query->join('LEFT', '#table_2 AS o ON a.id = o.item_id ORDER BY o.date DESC');
这没有效果
所以我现在已经困了好几个小时了。
编辑:
抱歉,这里是
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->select(
$this->getState(
'list.select',
'a.id AS id, a.title AS title, a.featured, a.date_created, a.users_id, a.published, '.
'c.title as category_title,a.date_publish,a.date_publish_down, o.how'
)
);
$query->from($db->quoteName('table_1').' AS a');
$query->join('LEFT', 'table_2 AS o ON a.id = o.item_id')->order('o.id');
$query->where('a.published = 1');
$query->group($db->escape('a.id'));
return $query;
以下查询将为您提供 MySQL
中的结果。它 may/may 不适用于其他 RDBMS。
在 select 语句中指定您想要的字段列表。
select * from
(select a.id, field_list_you_want from table1 a
left join table2 o ON a.id = o.item_id
order by a.id,o.date desc) temp
group by id
我一定是做错了什么,因为我已经尝试了此处搜索中出现的所有建议。
我正在尝试加入 table 1 和 table 2,但是从 table 2 我只想 select 1 条目 匹配 table 上的 id 1。但我不希望它从 table 2 中选择任何条目,它需要是最新日期。
我有
$query->join('LEFT', '#table_2 AS o ON a.id = o.item_id');
我试过了
$query->join('LEFT', '#table_2 AS o ON a.id = o.item_id')->order('o.date', 'DESC');
重新排序所有返回的结果并且对连接结果没有影响
我也试过了
$query->join('LEFT', '#table_2 AS o ON a.id = o.item_id ORDER BY o.date DESC');
这没有效果
所以我现在已经困了好几个小时了。
编辑:
抱歉,这里是
$db = $this->getDbo(); $query = $db->getQuery(true);
$query->select(
$this->getState(
'list.select',
'a.id AS id, a.title AS title, a.featured, a.date_created, a.users_id, a.published, '.
'c.title as category_title,a.date_publish,a.date_publish_down, o.how'
)
);
$query->from($db->quoteName('table_1').' AS a');
$query->join('LEFT', 'table_2 AS o ON a.id = o.item_id')->order('o.id');
$query->where('a.published = 1');
$query->group($db->escape('a.id'));
return $query;
以下查询将为您提供 MySQL
中的结果。它 may/may 不适用于其他 RDBMS。
在 select 语句中指定您想要的字段列表。
select * from
(select a.id, field_list_you_want from table1 a
left join table2 o ON a.id = o.item_id
order by a.id,o.date desc) temp
group by id