[英]How do I write a mysql query to get a list of records from one table with columns concatenated from multiple other tables
我的問題與此類似: MySQL concatenate values from a table into a record of another
但它不一樣,我想是因為我試圖利用來自其他幾個表的多個連接列。
這是我的表:
CREATE TABLE `Albums` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`userSettingsId` bigint(20) NOT NULL,
`name` varchar(100) NOT NULL,
`description` text DEFAULT NULL,
`isFavorite` tinyint(1) NOT NULL DEFAULT 0,
`isPublic` tinyint(1) NOT NULL DEFAULT 0,
`created` datetime NOT NULL DEFAULT current_timestamp(),
`lastEdited` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE KEY `Albums_UN` (`userSettingsId`,`name`),
CONSTRAINT `Albums_FK` FOREIGN KEY (`userSettingsId`) REFERENCES `UserSettings` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8mb4;
CREATE TABLE `AlbumsImages` (
`albumsId` bigint(20) NOT NULL,
`imagesId` bigint(20) NOT NULL,
`isCoverImage` tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`albumsId`,`imagesId`),
KEY `AlbumsImages_FK` (`imagesId`),
CONSTRAINT `AlbumsImages_FK` FOREIGN KEY (`imagesId`) REFERENCES `Images` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `AlbumsImages_FK_1` FOREIGN KEY (`albumsId`) REFERENCES `Albums` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `Collaborators` (
`albumsId` bigint(20) NOT NULL,
`access` enum('view','put','edit') NOT NULL DEFAULT 'view',
`email` varchar(100) NOT NULL,
PRIMARY KEY (`albumsId`,`email`),
CONSTRAINT `Collaborators_FK_1` FOREIGN KEY (`albumsId`) REFERENCES `Albums` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `Images` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`src` longtext NOT NULL,
`fileName` varchar(100) NOT NULL,
`alt` varchar(100) NOT NULL,
`userSettingsId` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `Images_UN_FileName_UserSettingsId` (`fileName`,`userSettingsId`),
KEY `Images_FK` (`userSettingsId`),
CONSTRAINT `Images_FK` FOREIGN KEY (`userSettingsId`) REFERENCES `UserSettings` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=91 DEFAULT CHARSET=utf8 COMMENT='All images in the application';
CREATE TABLE `UserSettings` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL,
`firstName` varchar(100) NOT NULL,
`lastName` varchar(100) NOT NULL,
`password` text NOT NULL,
`isDarkThemeEnabled` tinyint(1) NOT NULL DEFAULT 1,
`sessionId` varchar(255) DEFAULT NULL,
`profilePicture` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UserSettings_Email_UN` (`email`),
UNIQUE KEY `UserSettings_SessionId_UN` (`sessionId`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COMMENT='User Settings and Info'
注意: Collaborators
表不使用UserSettings.id
的外鍵,因為協作者不一定要有帳戶。
因此,我想獲取特定用戶的所有專輯和有關它們的輔助信息。 像這樣的東西:
[
{
id: 1,
name: 'Album Name',
description: 'Description',
isFavorite: 0,
created: '2021-04-26 23:14:05',
lastEdited: '2021-04-27 19:12:02',
images: [
{
fileName: 'image.jpg',
alt: 'Image Title',
src: 'www.image.com/image.jpg',
isCoverImage: 0,
}, //...etc
],
collaborators: [
{
email: 'someone@example.com',
firstName: null,
lastName: null,
id: null,
access: 'put',
},
{
email: 'someoneelse@example.com',
firstName: 'someone',
lastName: 'else',
id: 14,
access: 'view',
}, //...etc
],
}, //...etc
]
這是我目前正在使用的查詢。
SELECT
a.id,
a.name,
a.description,
a.isFavorite,
a.created,
a.lastEdited,
concat('[', group_concat(json_object(
'fileName', i.fileName,
'alt', i.alt,
'src', i.src,
'isCoverImage', ai.isCoverImage
)), ']') as images,
concat('[', group_concat(json_object(
'email', c.email,
'firstName', u.firstName,
'lastName', u.lastName,
'id', u.id,
'access', c.access
)), ']') as collaborators
from Albums a
left join AlbumsImages ai
on a.id=ai.albumsId
left join Images i
on i.id=ai.imagesId
left join Collaborators c
on c.albumsId = a.id
left join UserSettings u
on c.email = u.email
where a.userSettingsId=?
group by id;
Aaand 它確實有效......有點。 我得到了所有的專輯和他們的所有信息,但是合作者被圖像數量所復制,反之亦然。 作為現在的創可貼,我有一些在查詢后立即運行的重復數據刪除代碼,但這顯然是 hacky,而不是我想要長期 go 的東西。
有沒有辦法解決這個問題來做我想做的事,或者我是一個白痴,想首先在一個查詢中獲取所有這些信息?
謝謝!
所以你有一個協作者和圖像之間的笛卡爾積。 因此,兩者都乘以另一個結果的數量。
您可以運行多個查詢,然后將應用程序代碼寫入 append,將結果寫入更大的 JSON 文檔。
或者您可以使用相關子查詢:
SELECT
a.id,
a.name,
a.description,
a.isFavorite,
a.created,
a.lastEdited,
concat('[', (
select group_concat(json_object(
'fileName', i.fileName,
'alt', i.alt,
'src', i.src,
'isCoverImage', ai.isCoverImage))
from Images i where i.id=ai.imagesId
), ']') as images,
concat('[', (
select group_concat(json_object(
'email', c.email,
'firstName', u.firstName,
'lastName', u.lastName,
'id', u.id,
'access', c.access))
from Collaborators c
left join UserSettings u
on c.email = u.email
where c.albumsId=a.id
), ']') as collaborators
from Albums a
left join AlbumsImages ai
on a.id=ai.albumsId
where a.userSettingsId=?
group by id;
(未測試)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.