mysql联合查询有关问题

WBOY
Release: 2016-06-13 12:24:11
Original
789 people have browsed it

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;
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template