简体   繁体   English

如何在一个 mysql 查询中返回所有关系数据,并在按关系查询时停止仅连接一个结果

[英]How can I return all relationship data in one mysql query and stop only one result being concatanated when querying by relationship

I am trying to perform a MYSQL query which retrieves relationship data and concats it into an concatanated string of json objects which I later json_decode.我正在尝试执行 MYSQL 查询,该查询检索关系数据并将其连接到 json 对象的串联字符串中,我稍后会对其进行 json_decode。

I have set up a fiddle here but JSON_OBJECT does not work.我在这里设置了一个小提琴,但 JSON_OBJECT 不起作用。 The schema is there or below in markdown table.架构在 markdown 表中或下方。

The final result of what I would like to acheive is the results in the table below.我想要达到的最终结果是下表中的结果。

id ID title标题 date_published发布日期 artists艺术家 genres流派
1 1 My Song我的歌 2022-01-01 15:00:01 2022-01-01 15:00:01 [{id: 1, name: 'Pauli', is_featuring: 0}, {id: 2, name: 'Tony', is_featuring: 1}] [{id: 1, name: 'Pauli', is_featuring: 0}, {id: 2, name: 'Tony', is_featuring: 1}] [{id: 1, name: 'Rock'}, {id: 2, name: 'Pop'}] [{id:1,名称:'Rock'},{id:2,名称:'Pop'}]

Below is the query to retrieve songs based on the artist using the artist id.下面是使用艺术家 ID 根据艺术家检索歌曲的查询。 I get a list of songs but if there are more than one artist on the song it only returns the artist whose id is passed to the sql query.我得到了一个歌曲列表,但如果歌曲中有多个艺术家,它只返回其 id 传递给 sql 查询的艺术家。

Something similar happens when trying to do the same for querying by a genre.当尝试按流派查询时会发生类似的事情。

So my question is how do I return the data I want in the results but also perform a query on either the artist or genre?所以我的问题是如何在结果中返回我想要的数据,同时对艺术家或流派进行查询? I will also have to get a count of total records for the total number of results the query would return.我还必须计算查询将返回的结果总数的总记录数。

SELECT ms.id, ms.title, ms.date_published, 
CONCAT('[', GROUP_CONCAT(DISTINCT(JSON_OBJECT('id', ma.id, 'name', ma.name, 'is_featuring', mas.is_featuring_artist)) ORDER BY mas.id separator ','), ']') AS artists, 
CONCAT('[', IF(mg.id IS NULL, '', GROUP_CONCAT(DISTINCT(JSON_OBJECT('id', mg.id, 'name', mg.name)) ORDER BY mg.name separator ',')), ']') AS genres 
FROM songs ms 
LEFT JOIN artist_song mas ON mas.song_id = ms.id 
LEFT JOIN artists ma ON ma.id = mas.artist_id 
LEFT JOIN genre_song mgs ON mgs.song_id = ms.id 
LEFT JOIN genres mg ON mg.id = mgs.genre_id 
WHERE ma.id = 2 AND ms.published = 1 
GROUP BY ms.id
ORDER BY ms.date_published DESC 
LIMIT 10  
OFFSET 0;

This is the result I would get back.这是我要回来的结果。 Notice how the artist Pauli is no longer in the artists array as I query for an artist with the id of 2.请注意,当我查询 id 为 2 的艺术家时,艺术家 Pauli 不再在艺术家数组中。

id ID title标题 date_published发布日期 artists艺术家 genres流派
1 1 My Song我的歌 2022-01-01 15:00:01 2022-01-01 15:00:01 [{id: 2, name: 'Tony', is_featuring: 1}] [{id: 2, name: 'Tony', is_featuring: 1}] [{id: 1, name: 'Rock'}, {id: 2, name: 'Pop'}] [{id:1,名称:'Rock'},{id:2,名称:'Pop'}]

Below are the tables下面是表格

Songs Table歌曲表

id ID name姓名 published发表

Artists Table艺术家表

id ID name姓名

Artist Songs Table艺术家歌曲表

id ID artist_id艺术家 ID song_id歌曲编号 is_featured is_featured

Genres Table流派表

id ID name姓名

Genre Songs Table流派歌曲表

id ID genre_id流派_id song_id歌曲编号

You need to join with artist_song twice.你需要加入artist_song两次。 One of them restricts the songs that are selected, the other is to get all the artists on the song.其中一个限制选择的歌曲,另一个是让所有艺术家都上这首歌。

SELECT ms.id, ms.title, ms.date_published, 
CONCAT('[', GROUP_CONCAT(DISTINCT(JSON_OBJECT('id', ma.id, 'name', ma.name, 'is_featuring', mas.is_featuring_artist)) ORDER BY mas.id separator ','), ']') AS artists, 
CONCAT('[', IF(mg.id IS NULL, '', GROUP_CONCAT(DISTINCT(JSON_OBJECT('id', mg.id, 'name', mg.name)) ORDER BY mg.name separator ',')), ']') AS genres 
FROM songs ms 
JOIN artist_song mas1 ON mas1.song_id = ms.id
JOIN artist_song mas ON mas.song_id = ms.id 
JOIN artists ma ON ma.id = mas.artist_id 
LEFT JOIN genre_song mgs ON mgs.song_id = ms.id 
LEFT JOIN genres mg ON mg.id = mgs.genre_id 
WHERE mas1.artist_id = 2 AND ms.published = 1 
GROUP BY ms.id
ORDER BY ms.date_published DESC 
LIMIT 10  
OFFSET 0;

There's no need to join with artists twice, since you're not using anything from that table when restricting.无需两次加入artists ,因为在限制时您没有使用该表中的任何内容。 You can restrict the artist ID from mas1.artist_id .您可以从mas1.artist_id限制艺术家 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM