数据库,多表查询

select D.*,t1.TagName as unitName,t2.TagName as tasteName,t3.DishSortName ,S.ImgSrc
From(Select Row_Number() Over(Order By OrderID asc, CreateTime desc) As Rowid,* 
From Dish Where ShopID=6 and AppID=6 ) As D 
 left join DishTag as t1 on D.UnitID=t1.TagID and t1.ShopID=D.ShopID and t1.AppID=D.AppID  
 left join DishTag as t2 on D.TasteID=t2.TagID and t2.ShopID=D.ShopID and t2.AppID=D.AppID 
 left join DishSort as t3 on D.SortID=t3.DishSortID and t3.ShopID=D.ShopID and t3.AppID=D.AppID 
 left join StoreImg S on D.DishID= S.TargetID and D.ShopID=s.ShopID  and S.ImgType='菜品'
Where Rowid Between 1 And 15; 

Select Count(1) From Dish Where ShopID=6 and AppID=6 


在SQL里,常常需要对多个表关联起来进行查询,下面把我写的一个简单的多表关联的例子给大家看看,方法很简单,只要你学会原理就行:

select 
o.id id,o.oid oid,o.number number,o.seOrder seOrder,o.endprice endprice,--第一个表的字段
d.uid uid,d.oDatetime oDatetime,--第二个表的字段
p.proname proname,p.spec spec,p.material material,p.price price,--第三个表的字段
c.price1 price1,c.price2 price2,c.price3 price3,c.price4 price4,c.price5 price5 --第四个表的字段
from 
orderlist o --表一
left join products p on o.pid=p.id --表二
left join orderForm d on d.id=o.oid --表三
left join classify c on p.bid=c.id --表四
--更多的表
order by o.id desc

这样,就把四个表关联起来查询了。如果有更多的表,可以一个一个的关联下去,不过我还是不希望关联的表太多。



左外部连接 *= left  join

右外部连接=*  right join


外部连接中不匹配的分量用NULL表示

内连接用法

select a.*,b.* from a,b where a.id=b.id


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值