当前位置:Gxlcms > PHP教程 > mysql联合查询有关问题

mysql联合查询有关问题

时间:2021-07-01 10:21:17 帮助过:21人阅读

mysql联合查询问题
Persons" 表:
Id_P LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter homas Changan Street Beijing


"Orders" 表:
Id_O OrderNo Id_P
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 65


怎么查询Persons表id=1 对应Orders的数量

就是
Id_P(1) Adams 2
Id_P(2) Bush 0
Id_P(3) Carter 2
------解决思路----------------------
select Id_P(1) ,LastName from Persons,Orders where Id_O=Id_P;
------解决思路----------------------
select p.Id_P ,p.LastName,(select count(*) from Orders where Id_P=p.Id_P) as counter from Persons p
------解决思路----------------------
select count(Id_P) as shuliang from Orders where Orders.Id_P in (select Id_P from Persons) group by Id_P;

人气教程排行