mariadb 从毒物样式数据库中选择项目的标记名称

mpgws1up  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(130)

我敢肯定,这是问过,但我似乎找不到正确的谷歌短语。
我有三张table:
歌曲

| index   title     ...
-----------------------
| 'a001'  'title1'  ...
| 'a002'  'title2'  ...
...

标记Map

| index   item_index   tag_index
--------------------------------
|     1       'a001'      't001'
|     2       'a001'      't003'
|     3       'a001'      't004'
|     4       'a002'      't003'
|     5       'a002'      't005'
...

标签

| tag_index         name
------------------------
|    't001'        'foo'
|    't002'        'bar'
|    't003'     'foobar'
...

我正在努力想出一个查询,它将给予我这样的结果:

[0]: {index: 'a001', title: 'title1', tags: ['foo', 'foobar']}
[1]: {index: 'a002', title: 'title2', tags: ['foobar', 'something']}

所以我想要达到的是:

  • 查询releases表(类似于WHERE title = "abc"
  • 获取在同一行中返回的所有标记名

到目前为止,我已经实现了在同一行中返回所有标记索引,但没有获得实际的标记名。

[0]: {index: 'a001', title: 'title1', tags: ['t001', 't003']}

迄今为止,我的问题如下:

SELECT s.index, s.title, s.licensable, GROUP_CONCAT(tm.tag_index as tags) FROM songs s
LEFT JOIN tagmap tm ON s.index = tm.item_index
WHERE s.is_public = 1 GROUP BY s.catalogue_index ORDER BY s.release_date DESC

我应该注意到,没有直接的路线歌曲-〉标签,唯一的链接是标签Map。

vatpfxk5

vatpfxk51#

为了在提供的查询中获取标记名称而不是标记索引,您需要将标记Map表与标记表连接起来。您可以通过向查询添加另一个连接子句来实现此目的,如下所示:

SELECT s.index, s.title, GROUP_CONCAT(t.name) as tags
FROM songs s
LEFT JOIN tagmap tm ON s.index = tm.item_index
LEFT JOIN tags t ON tm.tag_index = t.tag_index
WHERE s.is_public = 1
GROUP BY s.index
ORDER BY s.release_date DESC

相关问题