我有这张桌子:
// mytable
+----+---------+
| id | related |
+----+---------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 1 |
| 5 | 3 |
| 6 | 2 |
| 7 | 4 |
| 8 | 4 |
| 9 | 2 |
+----+---------+
我只有$id = 6
这样的身份证号码。我想选择所有具有相同related
的行。所以这是预期的结果:
// newmytable
+----+---------+
| id | related |
+----+---------+
| 3 | 2 |
| 6 | 2 |
| 9 | 2 |
+----+---------+
我该怎么做?
这是我的查询但不起作用:
SELECT *
FROM mytable m
WHERE (SELECT related
FROM mytable
WHERE id = :id) t m.related = t.related
答案 0 :(得分:1)
这将有效
SELECT * FROM mytable WHERE related = (SELECT releated FROM mytable WHERE id = 6)
答案 1 :(得分:1)
select m2.id, m2.related
from
mytable m1
join mytable m2
on m2.related = m1.related
where m1.id = 6
答案 2 :(得分:1)
SELECT * FROM mytable
WHERE related = ( SELECT related FROM mytable WHERE id = 6)
未经测试......
答案 3 :(得分:1)
试试这个
select *from mytable
where related = (select related from mytable where id = 6)
答案 4 :(得分:1)
您好请用以下查询替换您的查询,您将获得预期的输出。
SELECT * FROM mytable WHERE =(SELECT related FROM mytable WHERE id =:id)