Home  >  Article  >  Backend Development  >  mysql联合查询有关问题

mysql联合查询有关问题

WBOY
WBOYOriginal
2016-06-13 12:24:11790browse

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;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn