简体   繁体   中英

How to relate or join the tables in sql

So i have two tables: artists and tracks. I want to relate them or join them so that i can select artists who did a specific track or track which had specific words in it. I was thinking that i need to relate the id's but i am not sure. These are the tables, and i have limited them up to 5 rows because they are too long

SELECT * FROM artists LIMIT 5; 
+----+----------------+------------+----------+----------------+--------+--------+
| id | name           | start_year | end_year | origin         | type   | gender |
+----+----------------+------------+----------+----------------+--------+--------+
|  4 | Massive Attack |       1987 |     NULL | United Kingdom | Group  | NULL   |
| 17 | Bob Dylan      |       1941 |     NULL | United States  | Person | Male   |
| 20 | Art of Noise   |       1983 |     2000 | United Kingdom | Group  | NULL   |
| 25 | Pavement       |       1989 |     2000 | United States  | Group  | NULL   |
| 29 | Stevie Wonder  |       1950 |     NULL | United States  | Person | Male   |
+----+----------------+------------+----------+----------------+--------+--------+

SELECT * FROM tracks LIMIT 5; 
+----+---------------------------------+----------+----------+----------+
| id | name                            | position | length   | album_id |
+----+---------------------------------+----------+----------+----------+
| 50 | Safe From Harm                  |        1 | 00:05:19 |        4 |
| 51 | One Love                        |        2 | 00:04:49 |        4 |
| 52 | Blue Lines                      |        3 | 00:04:22 |        4 |
| 53 | Be Thankful for What You've Got |        4 | 00:04:10 |        4 |
| 54 | Five Man Army                   |        5 | 00:06:04 |        4 |
+----+---------------------------------+----------+----------+----------+



SELECT * FROM albums LIMIT 5; 

+------+-------------------------+-----------+--------------+--------------+
| id   | name                    | artist_id | release_date | release_year |
+------+-------------------------+-----------+--------------+--------------+
|    4 | Blue Lines              |         4 | 1991-04-08   |         1991 |
|  335 | Madman Across the Water |       236 | 1971-11-05   |         1971 |
|  436 | The Singles 81>85       |       317 | 1985-10-15   |         1985 |
| 1271 | A Saucerful of Secrets  |       191 | 1968-06-29   |         1968 |
| 1281 | War                     |       197 | 1983-02-28   |         1983 |
+------+-------------------------+-----------+--------------+--------------+
5 rows in set (0.00 sec)

You haven't shown any desired results so the following is just a guess, based on

select artists who did a specific track

select ar.name
from tracks t
join albums a on a.id=t.album_id
join artists ar on ar.id=a.artist_id
where t.name='War'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM