我想计算表中重复次数最多的值,但是它是在子查询中完成的,我的SQLyog不支持这种类型的查询,下面是我的查询:
SELECT `band`.`band_id`,`member_id`,`member_name`,`contact_no`
FROM seasons
INNER JOIN programs_recorded ON programs_recorded.season_id=seasons.season_id
INNER JOIN programs ON programs_recorded.program_id=programs.program_id
INNER JOIN songs_performed ON songs_performed.program_id=programs.program_id
INNER JOIN songs ON songs.song_id=songs_performed.song_id
INNER JOIN `band` ON `band`.`song_id`=`songs`.`song_id`
INNER JOIN `band_member` ON `band_member`.`band_id`=`band`.`band_id`
WHERE `band`.`song_id` = ANY ( SELECT `songs_performed`.`song_id`
FROM `songs_performed`
GROUP BY `song_id`
HAVING COUNT(`s_id`)>3)
GROUP BY `member_id` ;
正如您在上面所看到的,我写了COUNT(s_id)>3
这不是通用的,一个解决方案是我使用GROUP BY函数以及order by和limit但是SQLyog给出了错误,说这个版本不支持它。
答案 0 :(得分:0)
无论如何,这样的事情有什么问题......
SELECT DISTINCT b.band_id
, member_id
, member_name
, contact_no
FROM seasons ss
JOIN programs_recorded pr
ON pr.season_id = ss.season_id
JOIN programs p
ON pr.program_id = p.program_id
JOIN songs_performed sp
ON sp.program_id = p.program_id
JOIN songs s
ON s.song_id = sp.song_id
JOIN band b
ON b.song_id = s.song_id
JOIN band_member bm
ON bm.band_id = b.band_id
JOIN
( SELECT sp.song_id
FROM songs_performed sp
GROUP
BY song_id
HAVING COUNT(s_id) > 3
) x
ON x.song_id = b.song_id;
...