当前位置:数据库 > MySQL >>

mysql两个表查询。。。高手指点

A表 aname from a 163 b 163 c sina d qq e sina B表 bname ispay number a 0 0 b 1 100 c 0 0 d 1 50 e 1 40 两个表唯一的关系是 name 要求查询出来的格式为 A表from的标识如163 qq sina A表from每一种有多少个 如:count(from) where from=163 取163的个数。 B表ispay=1的 在A表中name对应的from是163的ispay的个数 B表ispay=1的 在A表中name对用的from是163的number的和。 最终要求展现的形式,如上面A和B两个表查询最终结果为 from fromcount ispaycount number 163 2 1 100 sina 2 1 40 qq 1 1 50
追问:高手!感谢了。明显不行。B表中 ispay=1的条件去哪了。 A表中 from='163' 的条件去哪了。
答案:select a. form,a.formcount,b.ispaycount,b.number from (select a.form as form ,count(a.form) as formcount from a
 GROUP BY a.form ) a,
(select a.form as form,count(b.ispay) as ispaycount,sum(b.number) as number
 from a,b where a.aname=b.bname and b.ispay=1 GROUP BY a.form) b
where a.form=b.form  这样子写就可以了。
其他:select A.from,A.count(from),B.count(ispay),B.sum(number) from A,B group by A.form,B.ispay,b.number

试试吧,我手头没数据库 也不能试 

上一个:mysql 数据库 还原 时间
下一个:MySQL数据库创建表的SQL语言

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,