[英]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.