高利贷报警没用:sql两个表查询的问题

来源:百度文库 编辑:中科新闻网 时间:2024/05/01 22:45:30
就是有两个表A、B,A为父表
A表中stid字段,name字段,w字段,area字段
B表中stid字段,rn字段
-----------------
要查询A表中的name、w、area和B表中rn的最大值
-----------------
条件是:
1、A表字段的stid=B表字段的stid
2、A表中w字段有两种类型分别为1和2
3、A表中w字段1和2这两个类型中的表b的最大值
比如:
表A
Stid Name W area
1000 Tt 1 北京
1001 www 1 河南
1002 qq 2 上海
1003 Ee 2 青海

表B
Stid RN
1000 69
1001 50
1002 60
1003 58

我要查询的结果是
表A的stid,name,w的各个类型在表B中RN的最大值,如下:
stid=1000 Name = www w=1 RN=69
stid=1001 Name = qq w=2 RN=60
select stid,Name,w,max(B.m) as mmax
from B LEFT JOIN A ON A.stid = B.stid
group by A.w

select a.stid,name,max(b.rn) rn
from 表A a,表B b
where a.stid=b.stid
group by w

有错误哦!!
列 'a.STID' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
列 'a.name' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。

分组后在组内求最大值:
select a.stid,name,max(b.rn) rn
from 表A a,表B b
where a.stid=b.stid
group by w

select a.stid,a.naem,a.w,max(m) from a,b where a.stid=b.stid

select stid,Name,w,max(B.m) as mmax
from B LEFT JOIN A ON A.stid = B.stid
group by A.w

本来是会的~现在忘记了~~