这是我的查询
SELECT a.id,
a.venue_id,
a.user_id,
m1.profilenam AS user_profilename,
m1.photo_thumb AS user_photo_thumb,
m2.profilenam AS venue_profilename,
m2.photo_thumb AS venue_photo_thumb
FROM announce_arrival AS a
INNER JOIN members AS m1
ON a.user_id = m1.mem_id
INNER JOIN members AS m2
ON a.venue_id = m2.mem_id
GROUP BY a.venue_id, a.user_id
LIMIT 0,10
ORDER BY date DESC,
time DESC
我如何在此查询中使用count(*),我就像这样使用
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC;
但它的表现 计数(*) 7 3 1
我想要总数。
答案 0 :(得分:1)
您可以将查询包装到select count(*)中,如下所示:
SELECT COUNT(*) FROM
(SELECT a.id,a.venue_id, a.user_id, m1.profilenam as
user_profilename,m1.photo_thumb AS user_photo_thumb,m2.profilenam AS
venue_profilename, m2.photo_thumb AS venue_photo_thumb FROM announce_arrival
AS a INNER JOIN members as m1 ON (a.user_id = m1.mem_id) INNER JOIN members
as m2 ON (a.venue_id= m2.mem_id) GROUP BY a.venue_id, a.user_id)
答案 1 :(得分:1)
您可以将查询包装在另一个SELECT中:
SELECT COUNT(*) AS total FROM (
SELECT DISTINCT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
或者如果您想要所有DISTINCT COUNT(*)
的总和,请尝试:
SELECT SUM(cnt) AS total FROM (
SELECT DISTINCT COUNT(*) AS cnt
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.venue_id, a.user_id LIMIT 0,10 ORDER BY date DESC,time DESC) AS t
答案 2 :(得分:1)
要获取此行数,请包含a SQL_CALC_FOUND_ROWS选项中 SELECT语句,然后调用 之后是FOUND_ROWS():
mysql> SELECT SQL_CALC_FOUND_ROWS * FROM tbl_name WHERE id > 100 LIMIT 10;
mysql> SELECT FOUND_ROWS();
第二个SELECT返回一个数字 表示第一行的行数 如果它已经返回SELECT 没有LIMIT条款而写的。
即,在第一个查询中SQL_CALC_FOUND_ROWS
之后添加SELECT
,并将第二个查询替换为SELECT FOUND_ROWS()
。
答案 3 :(得分:0)
摆脱GROUP BY
,LIMIT
和ORDER
。如果你需要一个总计数,它们是没用的,没有意义(尤其是LIMIT
)。 DISTINCT
也没有意义。
SELECT COUNT(*)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
答案 4 :(得分:0)
试试这个
SELECT COUNT(a.user_id)
FROM announce_arrival AS a
INNER JOIN members as m1 ON (a.user_id = m1.mem_id)
INNER JOIN members as m2 ON (a.venue_id= m2.mem_id)
GROUP BY a.user_id LIMIT 0,10;
如果您正在使用计数,则无需通过
发出命令