【发布时间】:2012-12-25 16:29:47
【问题描述】:
我有两张桌子:
main : id_main, field1, filter
main_logs(5000 万行):auto inc、id_main、路径
我正在寻找以下结果: id_main, field1, 最常用的路径
我尝试了以下查询:
select id_main,
field1,
(select path, count(*) as cpt
from main_log
where main_log.id_main=main.id_main group by path order by cpt desc limit 1)
from main
where filter in (1,3,5);
Mysql 返回:操作数应包含 1 列
如果我删除路径,结果是正确的,但我错过了路径值。
select id_main,
field1,
(select path, count(*) as cpt
from main_log
where main_log.id_main=main.id_main group by path order by cpt desc limit 1)
from main
where filter in (1,3,5);
我不需要 count(*) 的结果,但我需要它用于“order by”
如何编写此查询以获得我的结果? 谢谢
主要
id_main | field1 | filter
1 | red | 1
2 | blue | 3
3 | pink | 1
main_logs
autoinc | id_main | path
1 | 1 | home1
2 | 1 | home2
3 | 1 | home2
4 | 2 | house2
5 | 2 | house7
6 | 2 | house7
7 | 3 | casee
预期结果
id_main | fields1 | most common path
1 | red | home2
2 | blue | house7
3 | pink | casee
【问题讨论】:
-
你能显示你需要的结果吗?
-
我需要 "main" 表中的每一行,包括 "main_log" 表中 id_main 的最常见路径
-
我认为stackoverflow.com/questions/12446368/… 中的答案提供了构建子查询的方法。然后您可以将其与
main表连接起来。 -
您正在尝试的查询是一个依赖查询,这将比我的和@Saharsh Shah 花费更多的时间,这是完美的
-
@benfromaix 在
main表中没有filter数据了