Mysql sql在线查询
我有一个SQL表 有条目Mysql sql在线查询,mysql,sql,line,aggregate,Mysql,Sql,Line,Aggregate,我有一个SQL表 有条目 +-----------------+------+ | idanother_table | Col1 | +-----------------+------+ | 11 | 50 | | 11 | 61 | | 11 | 62 | | 12 | 61 | | 12 | 62 | | 13 |
+-----------------+------+
| 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\u另一个表和col1=50
所以当一个id的col1=50时,我们取与该id相关的所有行
也许这个问题是另一个问题的重复,但实际上我不知道如何命名我的问题,因此我有任何研究基础您可以使用子查询上的联接来重新调整另一个表
select * from my_table
inner join (
select idanother_table
from my_table
where col1 = 50
) t on t.idanother_table = my_table.idanother_table
您可以尝试在
SELECT * FROM t
WHERE idanother_table IN
(
SELECT idanother_table
FROM t
where Col1 = 50
)
或存在
SELECT *
FROM t t1
WHERE exists
(
SELECT 1
FROM t tt
where tt.Col1 = 50 and t1.idanother_table = tt.idanother_table
)
SELECT t1.* FROM t t1
WHERE exists
(
SELECT 1
FROM t2
where t1.idanother_table =t2.idanother_table and t2.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 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)
向我们显示数据库模式、示例数据、当前和预期输出。请阅读,这里是一个学习如何提高问题质量和获得更好答案的好地方。我会使用IN-query,MySQL在使用相关子查询时往往优化得很糟糕。