我有2张这样的桌子
<IfModule mod_rewrite.c>
如何只加入按城市的订单ON ID排序的翻译的第一个city_id?
我想要下一个结果:
mod_rewrite
我正在尝试像这样的代码
mod_rewrite
但GROUP BY忽略了ORDER BY,并且在ORDER BY和没有一个
的情况下始终保持相同的行答案 0 :(得分:2)
有很多方法可以做到这一点,但这是一种方式!
SELECT
cities.*,
translates.*
FROM
cities
INNER JOIN
translates
ON
translates.city_id = cities.id
INNER JOIN
(
SELECT
city_id,
MIN(order) as order
FROM
translates
GROUP BY
city_id
) relevant_translates
ON
relevant_translates.city_id = translates.city_id
AND
relevant_translates.order = translates.order
;
relevant_translates
子查询获取翻译表中每个city_id值的最小订单值。然后通过使用翻译进行内部联接,您只需将翻译值保留为最小订单值。祝你好运!
答案 1 :(得分:0)
如果每个order
的{{1}}并非总是从city_id
开始,那么您可以使用相关子查询来获取最小的1
order
条记录对于每个城市:
translate
如果它确实从SELECT *
FROM `cities`
INNER JOIN `translates` t1 ON `cities`.`id` = t1.`city_id`
WHERE `order` = (SELECT min(`order`) FROM translates WHERE t1.city_id = translates.city_id);
开始,那么只需1
并完成它。
答案 2 :(得分:0)
这是一种方法:
SELECT c.id, c.
FROM cities c
INNER JOIN translates t ON cities.id = t.city_id
WHERE t.order = (SELECT MIN(order)
FROM translates t1
WHERE t1.city_id = c.id)