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

user2580401

I have a table like this (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)

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_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)

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.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to copy all records and add some changes in the same table in OracleDB

From Dev

How to Update a column of all duplicate records in mysql?

From Dev

Mysql, one column value changes bases on other column in same table

From Dev

Mysql get all records described in in condition even if it does not exists in table

From Dev

How to update a column with concatenate of two other column in a same table

From Dev

How to get all rows that has same ID but diffrent value in other column?

From Dev

MySQL - Max Value of Column to Calculate against ALL records in Same Column

From Dev

Get all records that "own" all of these other records

From Dev

How to skip last 2 records and get all other records with linq?

From Dev

how to get a field participating in 'n' records in a file using awk when some other field has same value in all those 'n' records?

From Dev

Get records not in table (MySQL)

From Dev

How to get the records back from two tables with same primary key if any of the other column fields are changed

From Dev

how to name the column same as records of column of another table in oracle

From Dev

How to get 2 id from tow column and get all records from another table

From Dev

Order records in table by other table column

From Dev

how to get all the records from MySQL table in php web services

From Dev

Update records based on other records in the same table

From Dev

MySQL - Max Value of Column to Calculate against ALL records in Same Column

From Dev

MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one

From Dev

Get records from the same table mysql

From Dev

How to write mysql innerquery when get the records count from same table

From Dev

How to get records from a table with two column have same value?

From Dev

In Mysql how to get records which for the same foreign key has all values showed up in a list for another col

From Dev

How to get all records from one table between dates of column in another table

From Dev

How to get the total sum of a column and get total records at the same time

From Dev

How to get reference to another object on other column on same row in a table in Javascript?

From Dev

Updating a column that depends on two other colums all in the same table

From Dev

Get fixed number of records from different column of the same table

From Dev

how to make a column that will store sum of all related records from another table in MySQL

Related Related

  1. 1

    How to copy all records and add some changes in the same table in OracleDB

  2. 2

    How to Update a column of all duplicate records in mysql?

  3. 3

    Mysql, one column value changes bases on other column in same table

  4. 4

    Mysql get all records described in in condition even if it does not exists in table

  5. 5

    How to update a column with concatenate of two other column in a same table

  6. 6

    How to get all rows that has same ID but diffrent value in other column?

  7. 7

    MySQL - Max Value of Column to Calculate against ALL records in Same Column

  8. 8

    Get all records that "own" all of these other records

  9. 9

    How to skip last 2 records and get all other records with linq?

  10. 10

    how to get a field participating in 'n' records in a file using awk when some other field has same value in all those 'n' records?

  11. 11

    Get records not in table (MySQL)

  12. 12

    How to get the records back from two tables with same primary key if any of the other column fields are changed

  13. 13

    how to name the column same as records of column of another table in oracle

  14. 14

    How to get 2 id from tow column and get all records from another table

  15. 15

    Order records in table by other table column

  16. 16

    how to get all the records from MySQL table in php web services

  17. 17

    Update records based on other records in the same table

  18. 18

    MySQL - Max Value of Column to Calculate against ALL records in Same Column

  19. 19

    MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one

  20. 20

    Get records from the same table mysql

  21. 21

    How to write mysql innerquery when get the records count from same table

  22. 22

    How to get records from a table with two column have same value?

  23. 23

    In Mysql how to get records which for the same foreign key has all values showed up in a list for another col

  24. 24

    How to get all records from one table between dates of column in another table

  25. 25

    How to get the total sum of a column and get total records at the same time

  26. 26

    How to get reference to another object on other column on same row in a table in Javascript?

  27. 27

    Updating a column that depends on two other colums all in the same table

  28. 28

    Get fixed number of records from different column of the same table

  29. 29

    how to make a column that will store sum of all related records from another table in MySQL

HotTag

Archive