使用一个子查询连接两个表

时间:2016-04-19 07:07:06

标签: mysql

我的数据库包含多个在一个或多个场所托管活动的小组。大多数时候它只是一个场地,但团体有时会有两个。 为简单起见,以下数据集很小且虚构:

'groups'table

+--------------+--------------+--------------+
| name         | pri_venue_id | alt_venue_id |
+--------------+--------------+--------------+
| Fast Fingers |            3 |            0 |
| Data Dishers |            4 |            0 |
| Leet Hacks   |            5 |            2 |
+--------------+--------------+--------------+

'场地'表

+----------+-------------------------+-----------------+
| venue_id | name                    | location        |
+----------+-------------------------+-----------------+
|        1 | Public Archives         | Querytown       |
|        2 | Storage Function Centre | Drive Bay       |
|        3 | Key Convention Centre   | Qwertyville     |
|        4 | Head-spin Mall          | Drive Park      |
|        5 | Fast Storage Facility   | Memory Bay      |
+----------+-------------------------+-----------------+

我想在一个查询中将两个表连接在一起,这样查询的结果将显示组,主要地点名称和位置,以及第二个地点和位置(如果有)。 在没有第二/替代场地的情况下,只显示主要,然后两个NULLS,如下:

Data Dishers, Head-spin Mall, Drive Park, NULL, NULL
Fast Fingers, Key Convention Centre, Qwertyville, NULL, NULL
Leet Hacks, Fast Storage Facility, Memory Bay, Storage Function Centre, Drive Bay

请注意,结果集不必以逗号分隔。

我已阅读其他stackoverflow问题,但尚未找到解决方案。我不断获得重复的行,或者除了替代场所之外的所有内容,它总是显示NULL。我不确定什么是最好的分组,正如我在另一个问题中看到的那样。也尝试使用联合,但似乎无法让它工作。

这是我提出的最接近的查询:

SELECT 
  DISTINCT groups. NAME,
    x. NAME,
    x.location,
    y. NAME,
    y.location
FROM groups
INNER JOIN (
    SELECT
        venues.*
    FROM venues
    INNER JOIN groups ON venue_id = pri_venue_id
) AS x,
groups
INNER JOIN (
    SELECT
        venues.*
    FROM venues
    LEFT JOIN groups ON venue_id = alt_venue_id
) AS y
GROUP BY x.venue_id
ORDER BY groups. NAME;

提前致谢。

2 个答案:

答案 0 :(得分:1)

一个INNER和一个LEFT JOIN以及CONCAT可以解决问题:

SELECT 
    CONCAT_WS(', ', `name`, `name1`, location1, name2, location2) groups 
FROM 
    (
        SELECT
            g.`name`,
            v1.`name` name1,
            v1.location location1,
            IF(v2.`name` IS NOT NULL, v2.`name`, 'NULL') name2,
            IF(v2.location IS NOT NULL, v2.location, 'NULL') location2
        FROM
            groups g
        INNER JOIN venues v1 ON v1.venue_id = g.pri_venue_id
        LEFT JOIN venues v2 ON v2.venue_id = g.alt_venue_id
        ORDER BY g.pri_venue_id
    ) x

输出:

+-----------------------------------------------------------------------------------+
| groups                                                                            |
+-----------------------------------------------------------------------------------+
| Fast Fingers, Key Convention Centre, Qwertyville, NULL, NULL                      |
| Data Dishers, Head-spin Mall, Drive Park, NULL, NULL                              |
| Leet Hacks, Fast Storage Facility, Memory Bay, Storage Function Centre, Drive Bay |
+-----------------------------------------------------------------------------------+
3 rows in set

如果需要,您可以使用真实'NULL'更改NULL字符串。

答案 1 :(得分:1)

您需要的是内连接和左连接。像这样......

requesting

coalesce函数基本上只是将request.abort值转换为字符串NULL