sql语句。连表查询

[复制链接]
查看11 | 回复4 | 2010-7-15 16:09:09 | 显示全部楼层 |阅读模式
我有三张表他们的关系分别是主表StudentInfo(stuId,stuNo,StuName,stuPwd其中stuid是主键),外表PunishInfo(pinfoId,StuId,PtId,PinfoTime(时间),pinfoNumber(次数)其中PinfoId是主键),它和另一张表 punishType(ptId,ptName(类型值),ptOrder(类型))值主外键关系(PunishInfo主表,punishType外键)现在我要查询SutdentInfo表中的stuNo,StuName,PunishInfo中的PinfoTime,PinfoNumber,punishType表中的ptName,ptOrder并根据ptOrder或者ptOrder分组
帮帮忙嘛。。各位哥哥。姐姐些最好用存储过程谢谢

回复

使用道具 举报

千问 | 2010-7-15 16:09:09 | 显示全部楼层
select a.stuNo,a.stuname,b.pinfotime,b.pinfonumber,c.ptname,c.ptorderfrom SutdentInfo a left join PunishInfo b on a.stuid=b.stuid left join punishType c on b.ptid=c.ptid group by c.ptorder用left可以保证后两表部分数据缺少也能查询出来。用inner第一表有数据,第二表或三表没数据那行就不显示了。数据被过滤掉了。right 第一表没数据时,第二表或三表有数据就能查询出来。建议用left,可以保证你的数据都查询出来,如需过滤加条件
回复

使用道具 举报

千问 | 2010-7-15 16:09:09 | 显示全部楼层
select a.stuNo,a.stuname,b.pinfotime,b.pinfonumber,c.ptname,c.ptorderfrom SutdentInfo a inner join PunishInfo b on a.stuid=b.stuid inner join punishType c on b.ptid=c.ptid
回复

使用道具 举报

千问 | 2010-7-15 16:09:09 | 显示全部楼层
你这个datetime是什么情况都统计数量了还要时间?那你是想要哪条的时间?不带时间这样写:SELECTu.*,u.username,(SELECTcount(1)FROMbrowse_historybhwhereu.id=bh.userid)ASBHSum,(SELECTcount(1
回复

使用道具 举报

千问 | 2010-7-15 16:09:09 | 显示全部楼层
select a.stuNo,a.StuName, b.PinfoTime,b.PinfoNumber, c.ptName,c.ptOrderfrom StudentInfo a inner join PunishInfo b on a.stuId = b.stuId inner join punishType c on b.ptId
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行