0

我继承了一个不应该更改的旧 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 wasPurchased=0?

任何帮助表示赞赏。

4

1 回答 1

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

不确定这是否是最好的方法(从来没有尝试过选择总和但是......),但我认为它会对你有所帮助。

于 2013-02-14T10:59:36.833 回答