[英]Mysql - How to get all records being in other column in the same table
I have a table like this (mytable): 我有一个这样的表(mytable):
+------------+-----------+----------------+
| id_mytable | id_artist | foreing_column |
+------------+-----------+----------------+
| 1 | 2 | 5 |
| 2 | 1 | 5 |
| 3 | 1 | 2 |
| 4 | 3 | 6 |
+------------+-----------+----------------+
but I only know the number 5 of the column foreign_column, knowing this 5, I can get all the id_artist(in this case 2 and 1) 但是我只知道foreign_column列的数字5,知道这个5,我可以得到所有的id_artist(在这种情况下为2和1)
SELECT id_artist FROM `artist_band` WHERE id_band= 5
so the problem now is that I want to get the foreign_column of in this case the two id_artist (1 ,2), so I will end up with this table: 所以现在的问题是,在这种情况下,我想获取两个id_artist(1,2)的foreign_column,所以我将得到此表:
+------------+-----------+----------------+
| id_mytable | id_artist | foreing_column |
+------------+-----------+----------------+
| 1 | 2 | 5 |
| 2 | 1 | 5 |
| 3 | 1 | 2 |
+------------+-----------+----------------+
(you know all the 2 and all the 1 being in foreign_column) (您知道所有2和所有1都在foreign_column中)
I've tried something like this: 我已经尝试过这样的事情:
(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
or: 要么:
SELECT * FROM `artist_band` where id_artist =
(SELECT id_artist FROM `artist_band` WHERE id_band= 5)
thanks. 谢谢。
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.