[英]Selecting data from 3 tables with limit
我有3个表ppc_offers,survery_offers,find_offers(来自管理部分,我将数据输入这些表中)
现在在前端,我想显示这些表中的最新3个报价。
也就是说,最新的3个会出现,它们可以来自单个表或多个表...它的条件是“最新3个”
我怎样才能做到这一点?
Laravel提供了任何方法吗?
还是我需要任何主表?
还是可以建议我对此进行常规SQL查询?
请帮忙。 谢谢。
PPC表:
-------------------------
id | integer
title | varchar
url | varchar
description | varchar
created_at | timestamp
updated_at | timestamp
--------------------------
调查表:
-------------------------
id | integer
title | varchar
created_at | timestamp
updated_at | timestamp
--------------------------
查找代码表:
-------------------------
id | integer
title | varchar
url | varchar
description | varchar
code | varchar
created_at | timestamp
updated_at | timestamp
--------------------------
使3个查询以相同的名称将它们合并为列
select * from
(
(query1 order by id desc limit 3) x
union
(query2 order by id desc limit 3) y
union
(query3 order by id desc limit 3) z
(
作为建议,将所有这3个要约都包含在一个表中,并使用“ offer_type” ID相互区分,而不是使用3个不同的表,可能更有意义。 它可以简化您的代码。
我相信这可能适用于您当前的设置:
$ppc = DB::table('ppc_offers')
->select('title', 'url', 'description', 'NULL as code', 'updated_at');
$survey = DB::table('survey_offers')
->select('title', 'NULL as url', 'NULL as description', 'NULL as code', 'updated_at');
$find = DB::table('find_offers')
->select('title', 'url', 'description', 'code', 'updated_at');
return $ppc->unionAll($survey)->unionAll($find)
->orderBy('updated_at', 'desc')
->take(3)
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.