我有这个问题:
SELECT itemID, item, note, sourceItemID, title, collectionName,
count(tagID) as qtTag,
count(modalityID) as qt_Modality, extra
FROM items INNER JOIN factors ON items.itemID = factors.zotero_itemID
WHERE (collectionID = :collectionID) AND (tagID = :tagID1 OR tagID = :tagID2) group by itemID, item, note, sourceItemID, title, collectionName, extra ORDER BY EXTRA;
现在,每个ITEM都连接到一个或多个TAGID。我需要的是以一种方式对结果进行排序,使得链接到TagID1和TagID2的ITEMS首先按顺序排列,其他的可以按EXTRA字段排序。 有可能吗? 我尝试过使用ORDER BY CASE语句,但我无法将其解决。
这是结果的样本:
如您所见,这些项目按EXTRA字段排序。但我需要首先将ITEMS链接到TagID1和TagID2,但这些字段不会显示在结果中。我只需要订单。
我曾尝试过这段代码,但无效:
SELECT itemID, item, note, sourceItemID, title, collectionName,
count(tagID) as qtTag,
count(modalityID) as qt_Modality, extra
FROM items INNER JOIN factors ON items.itemID = factors.zotero_itemID
WHERE (collectionID = :collectionID) AND (tagID = :tagID1 OR tagID = :tagID2) group by itemID, item, note, sourceItemID, title, collectionName, extra ORDER BY CASE WHEN tagID1 = :TagID1 THEN 1 WHEN tagID2 = :tagID2 THEN 2 END, EXTRA;
OB的。我正在使用Firebird 3.0数据库
答案 0 :(得分:1)
试试这个;
SELECT itemID, item, note, sourceItemID, title, collectionName,
count(tagID) as qtTag,
count(modalityID) as qt_Modality,
extra
FROM items INNER JOIN factors ON items.itemID = factors.zotero_itemID
WHERE (collectionID = :collectionID) AND (tagID = :tagID1 OR tagID = :tagID2)
group by itemID, item, note, sourceItemID, title, collectionName, extra, tagID
ORDER BY
(CASE
WHEN tagID = 'tagID1' THEN 1
WHEN tagID = 'tagID2' THEN 2
ELSE 3
END), extra