sql查询问题

pilishou 2008-11-18 10:08:15
好就没有碰数据库了,求大家给提点提点。
有两个表table1和table2
table1
id standardid standardname
1 111 qq
2 222 ee
3 333 rr
4 444 555
5 455 233
6 356 ddd
table2
id uperid context number
1 111 sss 11123
2 111 ddd 1222222
3 111 二 454555
4 222 等等等 11111
5 222 事实上 222222
6 333 等等等就 121222
7 444 等等等 122222
8 455 等等等 7383883
9 455 订单 39399393
10 356 事实上 3343444
我想查出的结果是:
standardid standardname uperid context number
111 qq 111 sss 11123
null null 111 ddd 122222
null null 111 二 454555
222 ee 222 等等等 11111
null null 222 事实上 222222
333 rr 333 等等等就 121222
444 555 444 等等等 122222
455 233 455 等等等 7383883
null null 455 订单 39399393
356 ddd 356 事实上 3343444
...全文
151 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
pilishou 2008-11-27
  • 打赏
  • 举报
回复
此贴无解~
tt2451 2008-11-19
  • 打赏
  • 举报
回复
写的有点麻烦,凑合能出来楼主要的结果,以后有时间再优化一下了。。。

select t1.standardid,t1.standardname,t4.uperid,t4.context,t4.number
from table1 t1 right join
(
select t3.temp2id,t2.uperid,t2.context,t2.number
from table2 t2 left join
(
select temp2.id as temp2id,temp2.uperid as temp2uperid from table2 temp2 group by temp2.uperid order by temp2.id
) t3
on t2.id=t3.temp2id
) t4
on t4.uperid=t1.standardid and t4.temp2id is not null


在mysql 下测试通过
tt2451 2008-11-19
  • 打赏
  • 举报
回复
写的有点麻烦,凑合能出来楼主要的结果,以后有时间再优化一下了。。。

select t1.standardid,t1.standardname,t4.uperid,t4.context,t4.number
from table1 t1 right join
(
select t3.temp2id,t2.uperid,t2.context,t2.number
from table2 t2 left join
(
select temp2.id as temp2id,temp2.uperid as temp2uperid from table2 temp2 group by temp2.uperid order by temp2.id
) t3
on t2.id=t3.temp2id
) t4
on t4.uperid=t1.standardid and t4.temp2id is not null


在mysql 下测试通过
mimi5211 2008-11-19
  • 打赏
  • 举报
回复
select a.standardid,a.standardname,b.updrid,b.context,b.number from table a,table b where a.id=b.id;
aa37742698 2008-11-18
  • 打赏
  • 举报
回复
select a.standardid,a.standardname,b.uperid,b.context,b.number from table1 a right join table2 b where b.uperid=a.standardid
getdate 2008-11-18
  • 打赏
  • 举报
回复
顶上去
pilishou 2008-11-18
  • 打赏
  • 举报
回复
右连接在结果中不会产生null值,我要的结果关键就在于要对应查出null值。
从右边查过去,当第一条记录出现匹配后全显示,第二条和上一条出现同一匹配记录后重复的字段全部显示null
tt2451 2008-11-18
  • 打赏
  • 举报
回复
楼主想查出的结果是:

standardid standardname uperid context number
111 qq 111 sss 11123
null null 111 ddd 122222
null null 111 二 454555
222 ee 222 等等等 11111
null null 222 事实上 222222
333 rr 333 等等等就 121222
444 555 444 等等等 122222
455 233 455 等等等 7383883
null null 455 订单 39399393
356 ddd 356 事实上 3343444


coolker007 2008-11-18
  • 打赏
  • 举报
回复
select a.standardid,a.standardname,b.uperid,b.context,b.number
from table1 a right join table2 b where b.uperid=a.standardid and a.id = b.id
tzb08 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 harryzyp 的回复:]
select a.standardid,a.standardname,b.uperid,b.context,b.number from table1 a right join table2 b where b.uperid=a.standardid

我也不熟,呵呵
[/Quote]

用这个
getdate 2008-11-18
  • 打赏
  • 举报
回复
学习
小米儿mikko 2008-11-18
  • 打赏
  • 举报
回复
select a.standardid ,a.standardname ,b.uperid ,b.context ,b.number from table1 a left join table2 b on a.id = b.id

harryzyp 2008-11-18
  • 打赏
  • 举报
回复
select a.standardid,a.standardname,b.uperid,b.context,b.number from table1 a right join table2 b where b.uperid=a.standardid

我也不熟,呵呵
dami01 2008-11-18
  • 打赏
  • 举报
回复
外连接,
jumpheightway 2008-11-18
  • 打赏
  • 举报
回复
弄个视图不就行了
luoguofeng 2008-11-18
  • 打赏
  • 举报
回复
select a.standardid,a.standardname,b.uperid,b.context,b.number from table1 a,table2 b where b.uperid=a.standardid
tt2451 2008-11-18
  • 打赏
  • 举报
回复
无正确解



帮顶

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧