我遇到一个问题,我只想显示一个查询,其中子查询的计数必须大于0。
我有这个SQL查询:
SELECT
t.id AS t__id,
t.state_code AS t__state_code,
t.number AS t__number,
t.meta AS t__meta,
t.date_created AS t__date_created,
(SELECT
COUNT(t2.id) AS t2__0
FROM tbl_messages t2
LEFT JOIN tbl_services t3
ON t2.service_id = t3.id
WHERE (t2.inbound_time BETWEEN "2011-01-31 16:00:00"
AND "2011-02-28 15:59:59"
AND t2.subscriber = t.number
AND t3.pool_id = 48)) AS t__0
FROM tbl_subscribers t
WHERE (TRUE)
ORDER BY t__0 DESC
LIMIT 100;
我想只显示t__0>的数据。 0 ...如何在此查询中应用它?
我尝试了这个查询,但它不起作用:
SELECT
t.id AS t__id,
t.state_code AS t__state_code,
t.number AS t__number,
t.meta AS t__meta,
t.date_created AS t__date_created,
(SELECT
COUNT(t2.id) AS t2__0
FROM tbl_messages t2
LEFT JOIN tbl_services t3
ON t2.service_id = t3.id
WHERE (t2.inbound_time BETWEEN "2011-01-31 16:00:00"
AND "2011-02-28 15:59:59"
AND t2.subscriber = t.number
AND t3.pool_id = 48)) AS t__0
FROM tbl_subscribers t
WHERE (t__0 > 0)
ORDER BY t__0 DESC
LIMIT 100
它会返回错误:
Query : SELECT t.id AS t__id, t.state_code AS t__state_code, t.number AS t__number, t.meta AS t__m...
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'having t__0 > 0
LIMIT 100' at line 19
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
答案 0 :(得分:0)
请改为尝试:
SELECT
t.id AS t__id,
t.state_code AS t__state_code,
t.number AS t__number,
t.meta AS t__meta,
t.date_created AS t__date_created,
COUNT(t2.id) AS t__0
FROM tbl_subscribers t
JOIN tbl_messages t2 ON t2.subscriber = t.number
LEFT JOIN tbl_services t3 ON t2.service_id = t3.id
WHERE t2.inbound_time BETWEEN "2011-01-31 16:00:00"
AND "2011-02-28 15:59:59"
AND t3.pool_id = 48
GROUP BY t.id, t.state_code, t.number, t.meta, t.date_created
HAVING COUNT(t2.id) > 0
ORDER BY t__0 DESC
LIMIT 100