sql在线查询

sql query on line

我有一个SQLtable 有条目

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

我想要一个给出这个结果的查询

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

因此获取与以下相关的所有行 id_anothertable and col1 = 50.

所以当我们有 col1=50 的 id 时,我们获取与该 id 相关的所有行

也许这个问题是另一个问题的重复,但我真的不知道如何命名我的问题所以我有任何研究基础

您可以对 col1 = 50 重新调整的子查询使用联接 idanother_table

select * from my_table 
inner join (
    select  idanother_table 
    from my_table  
    where col1 = 50  
) t on t.idanother_table = my_table.idanother_table 

你可以尝试使用IN

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

exists

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

您可以使用exists

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

我会在检查 col1 = 50:

的查询中使用 exists 运算符
SELECT *
FROM   mytable a
WHERE  EXISTS (SELECT *
               FROM   mytable b
               WHERE  a.idanother_table = b.idanother_table AND
                      b.col1 = 50)