gpt4 book ai didi

php - 如何在 group_concat 字段中检查 id?

转载 作者:行者123 更新时间:2023-11-29 01:38:59 26 4
gpt4 key购买 nike

我有一个从某个表返回文件及其标签和子类别的查询,我想获取具有特殊标签 ID 的文件。

SELECT file.id,file.title,content,comments,likes,visit,
group_concat(distinct sub_category_name ) as subCategoryName,
group_concat(distinct sub_category.id ) as subCatId,
GROUP_CONCAT(distinct tag_name order by tag_id) as tag_name,
GROUP_CONCAT(distinct tag_id order by tag_id) as tags_id
FROM file
LEFT JOIN file_subcat on file_subcat.file_id= file.id
LEFT JOIN sub_category on sub_category.id=file_subcat.subcat_id
LEFT JOIN tag_file tf1 on tf1.file_id=file.id
LEFT JOIN tag on tag_id=tag.id
group by file.id
HAVING tags_id LIKE ?
ORDER BY file.id DESC

如果我使用这个查询并且如果 tags_id 等于 6 则返回 6,16,...如何更改此查询以获取具有该标签 ID 的文件?而且我不想从基础更改查询,因为我想显示每个文件标签和子类别,而且这个查询看起来很有效,因为我不需要多个查询。结果集是这样的

file_id      tags_id
1 6,15,16
2 7,20,26
3 8,9,10

如果我在表中有这个,我可以在其中使用 In() 但是因为要得到这个结果我使用了 group by 和 having 并且它似乎不能在 having 子句中使用 IN 或 BETWEEN。

最佳答案

喜欢这个问题,试试这个,

SELECT file.id,file.title,content,comments,likes,visit,
group_concat(distinct sub_category_name ) as subCategoryName,
group_concat(distinct sub_category.id ) as subCatId,
GROUP_CONCAT(distinct tag_name order by tag_id) as tag_name,
GROUP_CONCAT(distinct tag_id order by tag_id) as tags_id
FROM file
LEFT JOIN file_subcat on file_subcat.file_id= file.id
LEFT JOIN sub_category on sub_category.id=file_subcat.subcat_id
LEFT JOIN tag_file tf1 on tf1.file_id=file.id
LEFT JOIN tag on tag_id=tag.id
group by file.id
HAVING FIND_IN_SET('16', tags_id)
ORDER BY file.id DESC

将 <'16'> 替换为您想要的任何 ID/Value,我相信您会获取它您还可以通过

排除 GROUP_CONCAT 中的特定值
 HAVING  !FIND_IN_SET('16', tags_id) 

关于php - 如何在 group_concat 字段中检查 id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31246910/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com