Search code examples
mysqlsqllineaggregate

sql query on line


I have one SQL table There are entries

+-----------------+------+
| idanother_table | Col1 |
+-----------------+------+
|              11 |   50 |
|              11 |   61 |
|              11 |   62 |
|              12 |   61 |
|              12 |   62 |
|              13 |   50 |
|              13 |   65 |
+-----------------+------+

I want a query that gives this result

+-----------------+------+
| idanother_table | Col1 |
+-----------------+------+
|              11 |   50 |
|              11 |   61 |
|              11 |   62 |
|              13 |   50 |
|              13 |   65 |
+-----------------+------+

So get all lines related to id_anothertable and col1 = 50.

So when for an id we have col1=50, we take all lines related to that id

Maybe this problem is a duplicate of another, but really i don't know how to name my problem so i have any base for research


Solution

  • You can try to use IN

    SELECT * FROM t 
    WHERE idanother_table IN 
    (
        SELECT idanother_table 
        FROM t
        where Col1 = 50
    )
    

    or exists

    SELECT * 
    FROM t t1 
    WHERE exists
    (
        SELECT 1 
        FROM t tt
        where tt.Col1 = 50 and t1.idanother_table = tt.idanother_table
    )