使用另一个查询查询 table table sql

query table using another lookup table sql

我正在查找 user_id 57 的用户存储的所有艺术家姓名和 URL。

Table "artists"

    id | name         | url
    ---+--------------+--------
    01 | Luke Combs   | url-1
    02 | Jason Aledan | url-2
    03 | Upchurch     | url-3
    04 | Lee Brice    | url-4
    05 | Khalid       | url-5

查找tableuser_artists:

    user_id | artist_id
   ---------+-----------
         02 | 05
         15 | 01
         37 | 01
         57 | 03
         57 | 01
         28 | 02

user_id=57 的期望结果:

    name        | url
    ------------+--------
    Luke Combs  | url-1
    Upchurch    | url-3

我的尝试:

SELECT name, url, user_id 
FROM artists, user_artists 
INNER JOIN user_artists.user_id ON artists.name 
WHERE user_id = 57; 

SQL 查询让我感到很困惑。我什至不确定 inner join 是否正确。我整个下午都在谷歌上搜索这些查询,最后通过 SQL 生成器得到了这个。任何帮助,非常感谢!

您没有提到 具体 RDBMS 这是为了什么 - 并且各种 RDBMS 在它们如何 "interpret" SQL 方面并非 100% 兼容标准 - 但我想像这样的东西 应该可以在任何 RDBMS 中工作 :

SELECT name, url, user_id 
FROM artists a
INNER JOIN user_artists ua ON ua.artist_id = a.id
WHERE ua.user_id = 57; 

基本上,您从 user_artists 中获取那些 user_id 与您想要的值匹配的行,然后通过 [=25= 加入 artists table ] 在 user_artists.artist_idartist.id 列之间 - 并产生您想要的输出。