我有两个模型需要得到工会选择。简单的unionAll工作,但如果我需要按选定列排序结果,它不工作。Laravel代码看起来像
$events = Event::select('id', 'event_name as name', 'publish_at', DB::raw("'EVENT' as type"))
->customer($id)
->orderBy('publish_at', 'asc');
$news = News::select('id', 'name', 'publish_at', DB::raw("'NEWS' as type"))
->customer($id)
->orderBy('publish_at', 'asc')
->unionAll($events)
->orderBy('publish_at');
此女生的SQL转储如下所示:
(
select "id", "name", "publish_at", 'NEWS' as type
from "news" where exists
(
select * from "customers"
inner join "news_customers"
on "customers"."id" = news_customers"."customer_id"
where "news"."id" = "news_customers"."news_id"
and "news_customers"."customer_id" = 3
)
order by "publish_at" asc
)
union all
(
select "id", "event_name" as "name", "publish_at", 'EVENT' as type
from "events" where exists
(
select * from "customers"
inner join "event_customers"
on "customers"."id" = "event_customers"."customer_id"
where "events"."id" = "event_customers"."event_id"
and "event_customers"."customer_id" = 3
)
order by "publish_at" asc
)
order by "publish_at" asc
1条答案
按热度按时间k5ifujac1#
可以使用子查询按
publish_at
列对联合结果排序。