我继承了一个旧的SQLite数据库我不应该改变(这是一个要求)。有很多表,但我会关注其中两个:
songs
----------
song_id (primary autoincrement)
group_id (external)
title
audio_file_path
wasPurchased (boolean, 0/1)
groups
----------
group_id (primary autoincrement, related to songs group_id)
group_name
目前,应用程序需要执行此查询:
SELECT song_id,title,audio_file_path,wasPurchased,G.group_name AS groupName,
G.group_id AS groupId FROM songs AS S JOIN groups AS G ON S.group_id=G.group_id
ORDER BY groupName DESC
有没有办法,使用相同的查询,提取有多少不同的G.group_id已被购买= 0?
感谢任何帮助。
答案 0 :(得分:0)
SELECT song_id,title,audio_file_path,wasPurchased,
G.group_name AS groupName, G.group_id AS groupId,
SUM (SELECT DISTINCT g.group_id
FROM yourtables/JOIN
WHERE wasPurchased = 0) as nb
FROM songs AS S
JOIN groups AS G ON S.group_id=G.group_id
ORDER BY groupName DESC
不确定这是否是最好的方法(从未尝试过总和中的选择但是......),但我认为它会对你有帮助。