我尝试在两个相同的表上使用count()来获得某种关系 这是我的查询
SELECT b.id, count(a) count(b.id) from
checkins_small a, checkins_small b
WHERE a.id =22 AND a.city = b.city AND a.date = b.date GROUP BY b.id ;
这是基于签到数据,我想找到所有在同一城市和同一日期登记的用户22所做的人。但是这个查询总是给我一个masive数字。 count(a.id)和count(b.id)的结果总是一样的如何解决这个问题?
SELECT a.id, count(a.id) from checkins_small a group by a.id order by count(a.id) DESC LIMIT 10;
此查询将给出以下结果:
userid checkin times
22 ; 1783
267 ; 1607
177 ; 1508
130 ; 1477
205 ; 1456
2 ; 1368
323 ; 1151
111 ; 1092
337 ; 940
53 ; 896
现在,我想找到所有与用户22在同一个日期和同一城市办理登机手续的人以及这些人签到的时间。所以结果将是这样的
user_id times
答案 0 :(得分:0)
SELECT
COUNT(b.ID) as CT,
b.date
FROM
checkins_small a
INNER JOIN
checkins_small b on b.city = a.city and b.date = a.date
WHERE
a.ID = 22
GROUP BY
b.date
答案 1 :(得分:0)
SELECT b.id AS user_id,
COUNT(b.id) AS times
FROM (SELECT DISTINCT
id,
city,
date
FROM checkins_small
WHERE id = 22) a,
checkins_small b
WHERE a.city = b.city
AND a.date = b.date
AND a.id <> b.id
GROUP BY b.id;