问个严肃的问题:left jion 的问题。
如下语句,我想用ORM怎么写
select line.id,line.title,line.price,t.adultprice ,
(case when t.adultprice is null then line.price else t.adultprice end ) as timeprice
from ctsto_line as line
left join
(SELECT o.
on line.id=t.lineid order by timeprice desc
图片直观点
我用原生的可以写出来
但是我想用
因为那个后面查询表表示查询表,就不知道怎么弄了。
有人知道的,分析下。谢谢加粗文字
不知道大家明白我意思没有,如果需要数据库表可以提供,如如下参考表
line表
islandprice表
好大一部分都是字段,可以省略。用到的是DB::raw的属性,还有就是默认是加前缀表,所以大家查询字段的时候记得加上。
select line.id,line.title,line.price,t.adultprice ,
(case when t.adultprice is null then line.price else t.adultprice end ) as timeprice
from ctsto_line as line
left join
(SELECT o.
adultprice,o.lineid FROM
ctsto_islandpriceas o where
departuredate>curdate() group by
lineidorder by departuredate asc) as t
on line.id=t.lineid order by timeprice desc
图片直观点
我用原生的可以写出来
但是我想用
因为那个后面查询表表示查询表,就不知道怎么弄了。
有人知道的,分析下。谢谢加粗文字
不知道大家明白我意思没有,如果需要数据库表可以提供,如如下参考表
line表
islandprice表
以上问题已经解决,使用了ORM解决了 代码如下
$line=$line->select(DB::raw('(case when ctsto_t.adultprice is null and ctsto_pt.adultprice is null then ctsto_line.price when ctsto_t.adultprice is null and ctsto_pt.adultprice is not null then ctsto_pt.adultprice else ctsto_t.adultprice end ) as timeprice'),
't.departuredate','pt.adultprice as yprice','t.adultprice','line.price','line.id','line.title','line.subtitle','line.thumbs','line.destid',
'line.flag','line.transport','line.origin','line.days','line.flight','line.info','line.extras','line.click','line.status','line.expired','line.seotitle','line.keywords','line.description','line.created_at'
,'line.updated_at','line.deleted_at','line.rank','line.linetype','line.cashcoupon','line.usecashcoupon','line.points','line.ordertype','line.schedule','line.lineclass','line.islandid','line.istop','line.toplevel'
,'line.islabel','line.indextop','line.listtop','line.plisttop','line.sort')
->leftJoin(DB::raw('(SELECT * FROM (select * from ctsto_islandprice where departuredate >curdate() order by departuredate asc ,adultprice asc) as ctsto_t2 group by lineid ) as ctsto_t') , function($join)
{
$join->on('line.id', '=', 't.lineid');
})
->leftJoin(DB::raw('(SELECT * FROM (select * from ctsto_price where departuredate >curdate() order by departuredate asc,adultprice asc) as ctsto_pt2 group by lineid ) as ctsto_pt') , function($join)
{
$join->on('line.id', '=', 'pt.lineid');
})
->where('line.destid',$this->mddata['destid'])->whereRaw('ctsto_line.deleted_at is null')->where('status','审核通过');
好大一部分都是字段,可以省略。用到的是DB::raw的属性,还有就是默认是加前缀表,所以大家查询字段的时候记得加上。
4 个回复
JohnLui
赞同来自: Mr_Jing
Mr_Jing
赞同来自:
娃娃脾气
赞同来自:
另外建议使用多表关联而不是join
空气
赞同来自: