mysql查询2列,引用不同表中的1列

时间:2013-04-08 16:05:58

标签: mysql join

很抱歉,如果有问题,但我无法将我发现的任何答案与我的情况联系起来。

我为这个问题创建了一个临时表:

describe temp;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id        | int(11)      | NO   |     | 0       |       |
| artist    | varchar(255) | NO   |     | NULL    |       |
| title     | varchar(255) | NO   |     | NULL    |       |
| id_genre  | int(11)      | NO   |     | NULL    |       |
| id_genre2 | int(11)      | NO   |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

另一个相关表是一个流派列表。

我想创建一个导致这两个查询组合的查询。

select temp.title,genre.name
from temp
join genre on genre.id = temp.id_genre;

+------------------------+--------------+
| title                  | genre1       |
+------------------------+--------------+
| Tight Capris - 1958-B  | Rockabilly   |
| Endless Sleep - 1958-5 | MyFavourites |
| Daisy Mae - 1966       | Rockabilly   |
| Fire of love - 1966-58 | Rockabilly   |
| Stormy - 1963          | Pop          |
+------------------------+--------------+
5 rows in set (0.00 sec)

select temp.title,genre.name as genre2
from temp
join genre on genre.id = temp.id_genre2;

+------------------------+------------+
| title                  | genre2     |
+------------------------+------------+
| Tight Capris - 1958-B  | Rockabilly |
| Endless Sleep - 1958-5 | Rockabilly |
| Daisy Mae - 1966       | Rockabilly |
| Fire of love - 1966-58 | Rockabilly |
| Stormy - 1963          | Pop        |
+------------------------+------------+
5 rows in set (0.00 sec)

注意1标题中的体裁差异:无尽的睡眠

我想创建一个看起来像这样的结果,除了用类型的名称替换genre.id:

+------------------------+----------+-----------+
| title                  | id_genre | id_genre2 |
+------------------------+----------+-----------+
| Tight Capris - 1958-B  |      163 |       163 |
| Endless Sleep - 1958-5 |      161 |       163 |
| Daisy Mae - 1966       |      163 |       163 |
| Fire of love - 1966-58 |      163 |       163 |
| Stormy - 1963          |       99 |        99 |
+------------------------+----------+-----------+
5 rows in set (0.00 sec)

由于

3 个答案:

答案 0 :(得分:1)

您需要两次加入表Genre,因为表temp上有两列依赖于它。

SELECT  temp.title,
        a.name GenreA,
        b.Name GenreB
FROM    temp
        INNER JOIN genre a 
            ON a.id = temp.id_genre
        INNER JOIN genre b 
            ON b.id = temp.id_genre2

输出

╔════════════════════════╦══════════════╦════════════╗
║         TITLE          ║    GENREA    ║   GENREB   ║
╠════════════════════════╬══════════════╬════════════╣
║ Tight Capris - 1958-B  ║ Rockabilly   ║ Rockabilly ║
║ Endless Sleep - 1958-5 ║ MyFavourites ║ Rockabilly ║
║ Daisy Mae - 1966       ║ Rockabilly   ║ Rockabilly ║
║ Fire of love - 1966-58 ║ Rockabilly   ║ Rockabilly ║
║ Stormy - 1963          ║ Pop          ║ Pop        ║
╚════════════════════════╩══════════════╩════════════╝

答案 1 :(得分:1)

这就是你需要的吗?

SELECT
    title,
    genre1.name as genre1,
    genre2.name as genre2
FROM 
    temp
INNER JOIN genre as genre1
    ON genre1.id = id_genre
INNER JOIN genre as genre2
    ON genre2.id = id_genre2

答案 2 :(得分:0)

我同意Stephan您需要两次加入流派表。 但是使用LEFT JOIN代替INNER JOIN可能是个好主意:

SELECT  temp.title,
        a.name GenreA,
        b.Name GenreB
FROM    temp
        LEFT OUTER JOIN genre a 
            ON a.id = temp.id_genre
        LEFT OUTER JOIN genre b 
            ON b.id = temp.id_genre2

如果你的临时记录在其中一个id_genre列中有NULL,或者你的id与流派表中的行不匹配,这将有所帮助。

添加了:

参考“如何根据特定类型进一步过滤。例如genre1 ='rockabilly'(不起作用)。”:

在并添加

HAVING genre1 = 'rockabilly'