日期:2014-05-19  浏览次数:20571 次

联表查询 取一条记录
情况是这样的,两张表……

产品表   tb_product
p_code(产品代号)   p_status(状态)
111                             7
222                             8
333                             9
此表p_code唯一,没有重复

使用情况表   tb_usemode
u_code(产品代号)   u_status(状态)
111                             1
111                             1
222                             3
222                             2
333                             0
此表u_code有重复,u_status也有重复  

如何联表查询出:
tb_product中p_status=8或tb_usemode中u_status=1的所有不重复记录
就是产品代号不能重复。

得到的结果应该是这样的
p_code     u_code     p_status     u_status
111           111           7                   1
222           222           8                   2(3)

如果u_status有两条,取其中一条即可。

谢谢各位,费心了。

------解决方案--------------------
select p_code, u_code, p_status , u_status=min(u_status)
from tb_product a inner join tb_usemode b where a.p_code=b.u_code
group by p_code, u_code, p_status