[英]How to select from a table based on another table in mysql
我有2张桌子
表A
id ifClosed
1 1
2 0
3 0
表B
id remittance
1 50.00
1 10.00
2 5.25
3 8.20
3 1.60
我需要从表 b 中选择所有记录,其中 ifClosed 列被标记为“0”
id remittance
2 5.25
3 8.20
3 1.60
我的审判:
select * from table B where tableA.ifclosed = '0'
使用 JOIN 子句
SELECT tableB.*
FROM tableB JOIN tableA ON tableB.id=tableA.id
WHERE tableA.ifClosed = 0
这是一个基本的 JOIN。 查看有关JOINS 的一些指导
select *
from TableB
inner join TableA
on TableA.id = TableB.id
where TableA.ifClosed = 0
检查这个:
select * from TableB b
where b.Id in (select a.Id from TableA a where a.IfClosed = '0')
使用left out join 以便只显示表b 中存在的id。 希望这可以帮助
SELECT tableB.* FROM tableB LEFT OUTER JOIN tableA ON tableB.id=tableA.id WHERE tableA.ifClosed = 0
select
*
from
TableB
join TableA on TableA.id = TableB.id and TableA.IfClosed = 0
不需要 where 子句
试试这个查询:
select * from tableB, tableA
where tableA.ifclosed = '0'
and tableA.id = tableB.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.