首页 >后端开发 >php教程 >mysql联合查询有关问题

mysql联合查询有关问题

WBOY
WBOY原创
2016-06-13 12:24:11829浏览

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;

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn