我有一个页面显示上传到我的系统上的文件的“文件标签”。每个文件都可以有 0、1 或多个与之关联的文件标签。
我已经成功创建了一个显示文件名及其标签的页面,例如:
File Tag(s)
File#1 None
File#2 TAG1
File#3 TAG1, TAG2
此 SQL 查询将有助于生成上述数据:
SELECT *, GROUP_CONCAT(`ftg_name` ORDER BY `ftg_name` SEPARATOR ', ')
AS `x_file_tag_names`
FROM `file` LEFT JOIN `link_file_filetag` ON `fileftg_file` = `file_id`
LEFT JOIN `filetag` ON `ftg_id` = `fileftg_filetag`
GROUP BY `fileftg_file` ORDER BY `file_created` DESC;
问题:
我刚刚在页面顶部实现了过滤器。过滤器允许我在系统中的不同文件标签之间进行选择。当有人选择过滤器时,我会在 WHERE 子句中添加一个条件。看看这个查询:
SELECT *, GROUP_CONCAT(`ftg_name` ORDER BY `ftg_name` SEPARATOR ', ')
AS `x_file_tag_names` FROM `file`
LEFT JOIN `link_file_filetag` ON `fileftg_file` = `file_id`
LEFT JOIN `filetag` ON `ftg_id` = `fileftg_filetag`
WHERE `fileftg_filetag` = '1'
GROUP BY `fileftg_file` ORDER BY `file_created` DESC;
这会在我的页面上产生这个结果:
File Tag(s)
File#1 None
File#2 TAG1
File#3 TAG1
The problem here is that I still want it to list TAG1, TAG2
for File#3
. But of course the WHERE condition is getting rid of the TAG2
.
I'm wondering if there is a solution that would involve only changing the SELECT condition. I was attempting to create a solution that looked something like:
SELECT *, IF(ISNULL(`filetag_file`), 'None',
(//a subquery that gets all file tags associated with file in outer query))