增强当前SQLite查询

增强当前SQLite查询,sqlite,Sqlite,我继承了一个旧的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)

我继承了一个旧的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

感谢您的帮助

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

不确定这是否是最好的方法(从未尝试过选择一个总和,但…),但我认为它会帮助你

我找到了一个不同的解决方案,有一个子查询(像你的一样),没有连接和求和:
SELECT COUNT(DISTINCT(group_id))FROM songs WHERE purchase=0)AS nb
。你的解决方案有点不同,所以我可能解释了我的需求。