嘿我正在尝试使用一个计数来显示乐队所拥有的数字总数,到目前为止我有:
SELECT cds.cd_id,cds.position,bands.band_name
FROM cds
INNER JOIN releases
INNER join bands
ON cds.cd_id =releases.cd_id and releases.band_id = bands.band_id
Where band_name IS NOT NULL AND position = 1;
SELECT bands.band_name, COUNT(band_name) AS Total FROM bands
WHERE band_name ='Prodigy';
这会将表格与位置cd_id和band_name连接在一起,然后使用计数来计算乐队' Prodigy'已经
cd020 1 Prodigy
cd021 1 Prodigy
cd024 1 Kylie Minogue
cd028 1 Kylie Minogue
cd031 1 Kylie Minogue
cd049 1 Kylie Minogue
cd065 1 Robbie Williams
cd068 1 Robbie Williams
cd070 1 Robbie Williams
正如你所看到的,Prodigy应该有2个1号位,我的计数只显示它有,我认为这是计算band_name出现次数的最简单方法,而不是每个频段的位置数。你能给予的任何帮助或建议都会非常有帮助
答案 0 :(得分:0)
聚合查询需要GROUP BY
语句。像这样:
SELECT bands.band_name, COUNT(band_name) AS Total FROM bands
WHERE band_name ='Prodigy'
GROUP BY bands.band_name;
要获得结果,请使用以下内容:
SELECT
bands.band_name,
COUNT(*)
FROM
cds
INNER JOIN
releases ON (cds.cd_id =releases.cd_id)
INNER JOIN
bands ON (releases.band_id = bands.band_id)
WHERE
bands.band_name IS NOT NULL
AND cds.position = 1
GROUP BY bands.band_name;
答案 1 :(得分:0)
这应该适合你。
SELECT cds.position, bands.band_name, COUNT(cds.cd_id) as COUNT
FROM cds
INNER JOIN releases
INNER join bands
ON cds.cd_id =releases.cd_id and releases.band_id = bands.band_id
WHERE band_name IS NOT NULL AND position = 1
GROUP BY bands.band_name, cds.position;