MySQL多对多关系选择条件
我有两个表 books 和 Tags,它们通过表 books_tags 处于多对多关系。我想要做的是选择所有具有一个或多个标签(如“%tag%”)的书籍。没问题。但是,我还有一个 group_concat 字段,我想用与所选书籍相关的所有标签填充该字段。
我的选择查询基本上如下所示:
select
flbdb_books.id,
flbdb_books.title,
flbdb_tags.id,
flbdb_tags.name,
group_concat(distinct concat('["', flbdb_tags.id, '","', flbdb_tags.name, '"]') separator ',') as tags
from
flbdb_books
join flbdb_books_tags
on flbdb_books.id = flbdb_books_tags.book_id
join flbdb_tags
on flbdb_tags.id = flbdb_books_tags.tag_id
group by flbdb_books.id;
如果我没有任何条件,这将提供所需的结果。我使用 PHP 和 json_decode 来读取标签字段。 我尝试了三种不同的方法来过滤结果,但都不起作用。
使用地点:
其中 flbdb_tags.name 如“%poetr%”
但是,这意味着标签字段(使用 group_concat 组装的字段)仅填充此标签
使用具有
flbdb_tags.name 类似于“%poetr%”
这不会返回所有标记为诗歌的书籍,因为在某些情况下,由于分组方式,诗歌标签隐藏在另一个标签后面
- 这不会返回所有标记为诗歌的书籍,因为在某些情况下,由于分组依据
使用 group_concat 字段
,具有“%poetr%”等标签
这实际上可以解决问题,但我需要进行不区分大小写的搜索,而 upper(tags) 不起作用
我希望我清楚我想要做什么。
谢谢
I have two tables books and tags which are in a many-to-many relationship via the table books_tags. What I want to do is select all books that have one or more tag(s) like '%tag%'. No problem. However, I also have a group_concat field which I want to be filled with all the tags that are related to the selected book.
My select query basically looks like this:
select
flbdb_books.id,
flbdb_books.title,
flbdb_tags.id,
flbdb_tags.name,
group_concat(distinct concat('["', flbdb_tags.id, '","', flbdb_tags.name, '"]') separator ',') as tags
from
flbdb_books
join flbdb_books_tags
on flbdb_books.id = flbdb_books_tags.book_id
join flbdb_tags
on flbdb_tags.id = flbdb_books_tags.tag_id
group by flbdb_books.id;
This delivers the desired result if I don't have any conditions. I use PHP and json_decode to read the tags field.
I have tried three different ways to filter the results, but none of them works.
use where:
where flbdb_tags.name like '%poetr%'
however, this means that the tags field (the one that is assembled using group_concat) is only filled with this tag
use having
having flbdb_tags.name like '%poetr%'
this does not return all books tagged poetry, because in some cases the poetry tag is hidden behind another tag due to the group by
use having on the group_concat field
having tags like '%poetr%'
this would actually do the trick, but I need to do the case-insensitive searching, and upper(tags) doesn't work
I hope it is clear what I want to do.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个关系查询可以完全满足您的需要。
我们的见解是,过滤器标签是您的起点,您实际上需要从标签连接到书籍,然后再连接到标签。
您需要加入两次,而不是一次,因为您过滤的标签是一组不同的数据,而不是一本书的所有标签。
There is a relational query that does exactly what you need.
The insight is that the filter tag is your starting point, and you actually need to join from the tag to the book back on to tags.
You need to join twice, not once since the tag you filter is a different set of data then all tags for a book.
它对你有用吗?
Will it work for you?
不确定我是否清楚理解,但我会尝试包装您的原始选择并在包装选择中进行过滤
Not sure I clearly understand, but I'd try to wrap your original select and do the filtering in the wrapper select