0

抱歉,如果有人提出问题,但我无法将我找到的任何答案与我的情况联系起来。

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

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)

谢谢

4

3 回答 3

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        ║
╚════════════════════════╩══════════════╩════════════╝
于 2013-04-08T16:08:01.767 回答
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
于 2013-04-08T16:10:44.510 回答
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

如果您在 temp 中的记录在 id_genre 列之一中具有 NULL,或者具有与流派表中的行不匹配的 id,这将有所帮助。

添加:

关于“我如何用特定的流派进一步过滤它。例如 wheregenre1 = 'rockabilly' (这不起作用)。”:

在并添加

HAVING genre1 = 'rockabilly'
于 2013-04-08T16:20:46.050 回答