SQL MariaDB从包含mm表的7个表中获取数据,从而导致过多的不需要的行

时间:2018-05-04 12:44:38

标签: sql mariadb

我在努力从7个不同的sql表中获取数据而没有收到太多行。

我有以下(简单)查询,它从7个不同的表中检索数据:

if(B == 0)
  A += 1;
++B;

正如您所看到的,表之间存在两个多对多关系。这两个表没有关系(除了通过h表)。现在的问题是,这导致为这些mm表的每个可能组合接收一行。

示例:

表1酒店

SELECT h.name, h.address, h.zipcode, h.city, h.association, r.name_de, f.first_name, f.last_name, f.email, p.year, j.name
FROM `tx_gipdhotels_domain_model_hotel` AS h

JOIN `tx_gipdhotels_hotel_jobs_mm` AS hj ON h.uid = hj.uid_local
JOIN `tx_gipdhotels_domain_model_jobs` AS j ON j.uid = hj.uid_foreign
JOIN `tx_gipdhotels_hotel_participations_mm` AS hp ON h.uid = hp.uid_local
JOIN `tx_gipdhotels_domain_model_participations` AS p ON p.uid = hp.uid_foreign
JOIN `tx_gipdhotels_domain_model_region` AS r ON r.uid = h.region
JOIN `fe_users` AS f ON f.uid = h.feuser

表2工作

|-----------|------------|----------|----------|
|    uid    |    name    |   jobs   |   part   |
|...........|............|..........|..........|
|     1     |     ab     |     3    |     2    |
|           |            |          |          |

表3部分

|-----------|------------|
|    uid    |    name    |
|...........|............|
|     1     |    tech    |
|     2     |     cs     |
|     3     |    perf    |
|           |            |

通过这种组合(包括hotel_jobs和hotel_part的mm表格),我只会收到一行酒店的6行,每行只有一行与另一行不同:

结果:

|-----------|------------|
|    uid    |    name    |
|...........|............|
|     1     |    abcd    |
|     2     |    efgh    |
|           |            |

如果我能在一行中检索这些数据,那将是很可爱的,如下所示:

想要的结果:

|-----------|------------|----------|----------|
|    uid    |    name    |   job    |   part   |
|...........|............|..........|..........|
|     1     |     ab     |   tech   |   abcd   |
|     1     |     ab     |   tech   |   defg   |
|     1     |     ab     |    cs    |   abcd   |
|     1     |     ab     |    cs    |   defg   |
|     1     |     ab     |   perf   |   abcd   |
|     1     |     ab     |   perf   |   defg   |
|           |            |          |          |

我无法弄清楚如何获得想要的结果,它超出了我的经验和知识,所以我问你,你知道如何用一个查询来实现这个目标吗?

我已经搜索了很多,我找到了STUFF()方法,但在MariaDB中不支持。在一些question here on stack中,有人做过与演员类似的事情,但我对此并不太了解,而且我也不知道如何适应我的问题...

我正在使用MariaDB,查询将来自php。无法更改表的数据结构。

非常感谢任何帮助和解释。

2 个答案:

答案 0 :(得分:2)

我希望这会有效,尝试一下,如果有任何错误,我们会解决它。

SELECT 
    h.name, 
    h.address,
    h.zipcode,
    h.city,
    h.association,
    GROUP_CONCAT(DISTINCT p.year SEPARATOR ', '),
    GROUP_CONCAT(DISTINCT j.name SEPARATOR ', '),
    r.name_de,
    f.first_name,
    f.last_name,
    f.email,
    h.tstamp,
    h.crdate 
    FROM tx_gipleasedisturbhotels_domain_model_hotel AS h
    JOIN `tx_gipleasedisturbhotels_hotel_jobs_mm` AS hj 
    ON h.uid = hj.uid_local
    JOIN `tx_gipleasedisturbhotels_domain_model_jobs` AS j 
    ON j.uid = hj.uid_foreign
    JOIN `tx_gipleasedisturbhotels_hotel_participations_mm` AS hp 
    ON h.uid = hp.uid_local
    JOIN `tx_gipleasedisturbhotels_domain_model_participations` AS p 
    ON p.uid = hp.uid_foreign
    JOIN `tx_gipleasedisturbhotels_domain_model_region` AS r 
    ON r.uid = h.region
    JOIN `fe_users` AS f 
    ON f.uid = h.feuser
    GROUP BY h.name
    ORDER BY h.name ASC

答案 1 :(得分:0)

感谢@jarlh我找到了解决方案:

SELECT h.name, h.address, h.zipcode, h.city, h.association,
GROUP_CONCAT(DISTINCT p.year SEPARATOR ', '),
GROUP_CONCAT(DISTINCT j.name SEPARATOR ', '),
r.name_de, f.first_name, f.last_name, f.email, h.tstamp, h.crdate

FROM `tx_gipleasedisturbhotels_domain_model_hotel` AS h

JOIN `tx_gipleasedisturbhotels_hotel_jobs_mm` AS hj ON h.uid = hj.uid_local
JOIN `tx_gipleasedisturbhotels_domain_model_jobs` AS j ON j.uid = hj.uid_foreign
JOIN `tx_gipleasedisturbhotels_hotel_participations_mm` AS hp ON h.uid = hp.uid_local
JOIN `tx_gipleasedisturbhotels_domain_model_participations` AS p ON p.uid = hp.uid_foreign
JOIN `tx_gipleasedisturbhotels_domain_model_region` AS r ON r.uid = h.region
JOIN `fe_users` AS f ON f.uid = h.feuser

GROUP BY h.name
ORDER BY h.name ASC

它是GROUP_CONCAT GROUP BY的组合。它必须按您希望只有一次的字段进行分组。要将所有mm值赋予一个单元格,您必须在SELECT语句中的这些字段上使用GROUP_CONCAT。

通过此查询,我收到了想要的结果。也许这对其他人也有帮助。 ;)