抱歉,如果有人提出问题,但我无法将我找到的任何答案与我的情况联系起来。
我为这个问题创建了一个临时表:
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)
谢谢