2Been花了几个小时在这上面,它对我来说没问题。我究竟做错了什么。我的数据库看起来像这样。
id, type_name
1, 1
2, 3
3, 1
4, 1
5, 2
6, 3
我的查询看起来像这样。
SELECT
DISTINCT(p.`id`) as id,
count (p1.type_name) as L1,
count (p2.type_name) as L2,
count (p3.type_name) as L3
FROM
proxylist AS p
LEFT JOIN (SELECT `id`,`type_name`, count(`type_name`) as L1
WHERE `type_name` = 1) as p1
ON p1.`id` = p.`id`
LEFT JOIN (SELECT `id`,`type_name`, count(`type_name`) as L1
WHERE `type_name` = 3) as p2
ON p2.`id` = p.`id`
LEFT JOIN (SELECT `id`,`type_name`, count(`type_name`) as L1
WHERE `type_name` = 3) as p3
ON p3.`id` = p.`id`
GROUP BY p.`id`
我收到错误" [Err] 1064 - 您的SQL语法出错了;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以便在' count(type_name
)附近使用为L1
WHERE type_name
= 1)为p1
在第1页。id
= p。id
"
答案 0 :(得分:1)
我认为你只想要条件聚合:
SELECT p.id,
SUM(p.type_name = 1) as L1,
SUM(p.type_name = 2) as L2,
SUM(p.type_name = 3) as L3
FROM proxylist p
GROUP BY p.id;
我相当确定您的查询会针对三个计算值返回1
或0
。以上是对你可能要做的事情的合理猜测。