繁体   English   中英

MySQL-如何获取所有记录在同一表的其他列中

[英]Mysql - How to get all records being in other column in the same table

我有一个这样的表(mytable):

+------------+-----------+----------------+
| id_mytable | id_artist | foreing_column |
+------------+-----------+----------------+
|          1 |         2 |              5 |
|          2 |         1 |              5 |
|          3 |         1 |              2 |
|          4 |         3 |              6 |
+------------+-----------+----------------+

但是我只知道foreign_column列的数字5,知道这个5,我可以得到所有的id_artist(在这种情况下为2和1)

SELECT id_artist FROM `artist_band` WHERE id_band= 5

所以现在的问题是,在这种情况下,我想获取两个id_artist(1,2)的foreign_column,所以我将得到此表:

    +------------+-----------+----------------+
    | id_mytable | id_artist | foreing_column |
    +------------+-----------+----------------+
    |          1 |         2 |              5 |
    |          2 |         1 |              5 |
    |          3 |         1 |              2 |
    +------------+-----------+----------------+

(您知道所有2和所有1都在foreign_column中)

我已经尝试过这样的事情:

(SELECT id_artist FROM `artist_band` as one WHERE id_band= 5)

inner join

(SELECT * FROM `artist_band` as two )

on one.id_artist = two.id_artist

要么:

SELECT * FROM `artist_band` where id_artist =

(SELECT id_artist FROM `artist_band` WHERE id_band= 5)

谢谢。

SELECT *
FROM mytable
WHERE id_artist 
IN 
 (
    SELECT id_artist 
    FROM mytable
    WHERE foreign_column = 5 -- (or whatever)
 )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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