什么样的人会写这样的sql语句(实际工程中)

lyllirui 2006-10-18 11:19:11
select fir.name as topcataname,
sec.name as seccataname,
thd.name as thdcataname,
p.title,
up.hasproduct,
up.price,
up.inputdate
from userproduct up
inner join product p on up.productid=p.productid
inner join cata thd on p.catano=thd.catano
inner join cata sec on thd.upcatano= sec.catano
inner join cata fir on sec.upcatano= fir.catano ";
...全文
387 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
十一月猪 2006-10-22
  • 打赏
  • 举报
回复
玩笑
十一月猪 2006-10-22
  • 打赏
  • 举报
回复
去扁人吧
zsl5305256 2006-10-22
  • 打赏
  • 举报
回复
我看那代码没错啊!你可以用视图看一下!
panqifeng440 2006-10-22
  • 打赏
  • 举报
回复
呵呵,还蛮好玩的,lyllirui(小李)有意思的人。
蔡健常熟 2006-10-21
  • 打赏
  • 举报
回复
很多不同类的数据写在了同一个表的同一个字段
蔡健常熟 2006-10-21
  • 打赏
  • 举报
回复
有这样的用法,不过可能是数据库设计时偷懒了。
liangpei2008 2006-10-21
  • 打赏
  • 举报
回复
把代码放在视图设计器中看一下可视化关系不就清楚了嘛!
njz168 2006-10-21
  • 打赏
  • 举报
回复
lyllirui(小李)
根据代码的意思,的确是要原来那样写的。
同表关联,必须用别名才能区别记录集。
关联时,并不需要表中全部内容,而是符合条件的记录集了,别名就表示该记录集的意思。
lyllirui 2006-10-19
  • 打赏
  • 举报
回复
njz168(飞龙在天) 出来说说话,是你无意看错了还是我的理解是错误的,这很关键,我已经大骂了写这段代码的人,如果真的是我理解有误,我还得跟人家道歉
lyllirui 2006-10-19
  • 打赏
  • 举报
回复
//原来代码
select fir.name as topcataname,
sec.name as seccataname,
thd.name as thdcataname,
p.title,
up.hasproduct,
up.price,
up.inputdate
from userproduct up
inner join product p on up.productid=p.productid
inner join cata thd on p.catano=thd.catano
inner join cata sec on thd.upcatano= sec.catano
inner join cata fir on sec.upcatano= fir.catano ";

//这是我解析出来的意思
select cata.name as topcataname,
cata.name as seccataname,
cata.name as thdcataname,
product.title,
userproduct.hasproduct,
userproduct.price,
userproduct.inputdate
from userproduct up
inner join product p on userproduct.productid=product.productid
inner join cata thd on product.catano=cata.catano
inner join cata sec on cata.upcatano= cata.catano
inner join cata fir on cata.upcatano= cata.catano ";

//进一步简化。可以得到
select
cata.name,
cata.name ,
cata.name,
product.title,
userproduct.hasproduct,
userproduct.price,
userproduct.inputdate
from userproduct
inner join product on userproduct.productid=product.productid
inner join cata on product.catano=cata.catano
inner join cata on cata.upcatano= cata.catano
inner join cata on cata.upcatano= cata.catano;

大家看看我理解正不正确?
lyllirui 2006-10-19
  • 打赏
  • 举报
回复
楼上的理解不对吧,fir、sec、thd都是cata的别名啊?
inner join cata sec on thd.upcatano= sec.catano
inner join cata fir on sec.upcatano= fir.catano ";
这两句替换后不就是cata.upcatano=cata.catano;然后重复了一遍么?
select fir.name as topcataname,
sec.name as seccataname,
thd.name as thdcataname,
不都是选择数据库中同一字段么?
njz168 2006-10-19
  • 打赏
  • 举报
回复
select fir.name as topcataname,
sec.name as seccataname,
thd.name as thdcataname,
p.title,
up.hasproduct,
up.price,
up.inputdate
from userproduct up
inner join product p on up.productid=p.productid
inner join cata thd on p.catano=thd.catano
inner join cata sec on thd.upcatano= sec.catano
inner join cata fir on sec.upcatano= fir.catano ";

inner join好办,不要写这么难看的代码:

个人认为容易理解些的如下:

select fir.name as topcataname,sec.name as seccataname,thd.name as thdcataname,
p.title,up.hasproduct,up.price,up.inputdate
from userproduct up,product p,cata thd,cata sec,cata fir
where up.productid=p.productid and p.catano=thd.catano and thd.upcatano= sec.catano
and on sec.upcatano= fir.catano
改写之后,还是难看,呵呵。
cata 一个表记录着三个级别的东西,却用三个字段来区分。不如考虑用两个字段来区别,一个是parent,一个是层,其实有一个parent就行了。
查找结果是用户产品的价格,注册时间以及产品名称以及三个cata(这是什么意思)级别的名称。



lyllirui 2006-10-18
  • 打赏
  • 举报
回复
自己表内的upcatano=catano,然后下一行转身catano=upcatano
lyllirui 2006-10-18
  • 打赏
  • 举报
回复
inner join cata thd on p.catano=thd.catano
inner join cata sec on thd.upcatano= sec.catano
inner join cata fir on sec.upcatano= fir.catano ";
这几句他一定是在调戏我了,一个表定义三个别名,fir÷sec÷thd都是一个表的别名
lyllirui 2006-10-18
  • 打赏
  • 举报
回复
楼上兄台的意思是他不是在调戏我?
marco08 2006-10-18
  • 打赏
  • 举报
回复
SQL语句没问题,只是表之间的关系复杂
lyllirui 2006-10-18
  • 打赏
  • 举报
回复
我还真的怀疑自己会错怪他,我已经5年多没认真做过数据库了。所以请大家帮我看看,麻烦解释一下!
lyllirui 2006-10-18
  • 打赏
  • 举报
回复
真的说说,我要回去打人了
marco08 2006-10-18
  • 打赏
  • 举报
回复
哈哈
lyllirui 2006-10-18
  • 打赏
  • 举报
回复
难道是我不懂数据库了?难道现在就流行这样的代码?我拿出来大家帮我想想,免得回头错扁了他。
加载更多回复(1)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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