0

我已经建立了两个多对多关系“结构”。一个是针对电影及其氛围的:

CREATE TABLE Films (  
id INT NOT NULL AUTO_INCREMENT,  
PRIMARY KEY(id),    
Title VARCHAR(255));

CREATE TABLE Ambiences (  
id INT NOT NULL AUTO_INCREMENT,   
PRIMARY KEY(id),  
ambienceName VARCHAR(255));

CREATE TABLE Films_Ambiences (
film_id INT NOT NULL,  
ambience_id INT NOT NULL,  
PRIMARY KEY (film_id, ambience_id),  
FOREIGN KEY (film_id) REFERENCES Films(id) ON UPDATE CASCADE,  
FOREIGN KEY (ambience_id) REFERENCES Ambiences(id) ON UPDATE CASCADE);

第二个是针对电影及其细节的:

CREATE TABLE Specifics (  
id INT NOT NULL AUTO_INCREMENT,   
PRIMARY KEY(id),  
specificName VARCHAR(255));

CREATE TABLE Films_Specifics (film_id INT NOT NULL,
specific_id INT NOT NULL,
PRIMARY KEY (film_id,specific_id),
FOREIGN KEY (film_id)
REFERENCES Films(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (specific_id)
REFERENCES Specifics(id) ON UPDATE CASCADE ON DELETE CASCADE);

我想在一张表中显示所有结果(按电影标题分组)。换句话说,我想要:

+-------+--------------+------------------+
| 标题 | 氛围 | 规格 |
+-------+--------------+------------------+
| 电影1 | 好笑,吓人| 外星人 |
| 电影2 | 伤心,可怕 | 骑士,外星人| 高分辨率照片| CLIPARTO
| 电影3 | 搞笑 | 骑士 |
| . | | |
| . | | |
| . | | |
+-------+--------------+------------------+

仅使用环境,我使用了一些 JOIN 和 GROUP_CONCAT,如下所示:

SELECT *,GROUP_CONCAT(ambienceName SEPARATOR ', ') AS ambiences
FROM Films AS f 
INNER JOIN Films_Ambiences as fa ON f.id = fa.film_id           
INNER JOIN Ambiences AS a ON a.id = fa.ambience_id
GROUP BY Title

在环境和细节方面,我尝试使用两个 GROUP_CONCAT,但没有成功。如果您愿意,
这是一个SQLFiddle 。

这里的另一个问题是,如果我有三个或更多多对多“结构”怎么办?这有意义吗?

更新

我还想展示没有具体细节的电影。@Kickstart 提出的解决方案似乎不包括这一点。

换句话说,我想展示一部funny, sad没有细节的电影。这是一个更新的SQLFiddle - 你会发现没有选择 film4。

4

2 回答 2

1

也许在 GROUP_CONCAT 中使用 DISTINCT

SELECT *,
GROUP_CONCAT(DISTINCT ambienceName SEPARATOR ' ') AS ambiences, 
GROUP_CONCAT(DISTINCT specificName SEPARATOR ' ') AS specifics 
FROM Films AS f 
LEFT OUTER JOIN Films_Ambiences as fa ON f.id = fa.film_id          
LEFT OUTER JOIN Ambiences AS a ON a.id = fa.ambience_id
LEFT OUTER JOIN Films_Specifics as fs ON f.id = fs.film_id          
LEFT OUTER JOIN Specifics AS s ON s.id = fs.specific_id
GROUP BY Title
于 2013-07-12T20:25:58.683 回答
0

试试这个:

select Title,
GROUP_CONCAT(distinct(trim(AMBIENCENAME)),',')as Ambience,
GROUP_CONCAT(distinct(trim(specificName)),',')as Specifics
from Films f join 
Films_Ambiences fa on f.id = fa.film_id join
Ambiences a on a.id=fa.ambience_id join
Films_Specifics fs on fs.film_id = f.id join
Specifics s on s.id = fs.specific_id
group by title;

结果几乎与您说要保存一些杂散逗号的结果相同。

____________________________________________
|TITLE  |AMBIENCE       |SPECIFICS         |
|-------|---------------|------------------| 
|film1  |sad,           |aliens,           |
|film2  |sad,,happy,    |monsters,         |
|film3  |happy,         |monsters,,aliens, |
--------------------------------------------

编辑:满足以下更新要求的新查询

select Title,
GROUP_CONCAT(distinct(AMBIENCENAME),',')as Ambience,
CASE WHEN specificName is null then 'none' else GROUP_CONCAT(distinct(specificName ),',') END as Specifics
from 
Films f left join
Films_Ambiences fa on f.id = fa.film_id left join
Ambiences a on a.id=fa.ambience_id left join
Films_Specifics fs on fs.film_id = f.id left join
Specifics s on s.id = fs.specific_id
group by title; 

结果:

__________________________________________
|TITLE  |AMBIENCE      |SPECIFICS        |
|----------------------------------------|
|film1  |sad,          |aliens,          |
|film2  |sad,,happy,   |monsters,        |
|film3  |happy,        |aliens,,monsters,|
|film4  |sad,,happy,   |none             |
------------------------------------------
于 2013-07-12T20:44:33.963 回答