简体   繁体   English

从3个表中选择数据限制

[英]Selecting data from 3 tables with limit

I have 3 tables ppc_offers, survery_offers,find_offers ( from admin section, i enter data into these tables) 我有3个表ppc_offers,survery_offers,find_offers(来自管理部分,我将数据输入这些表中)

Now on front end, i want to display latest 3 offers from these tables. 现在在前端,我想显示这些表中的最新3个报价。

ie latest 3 will come, they can be from single table or multiple tables... it just has condition "latest 3" 也就是说,最新的3个会出现,它们可以来自单个表或多个表...它的条件是“最新3个”

How can i do this? 我怎样才能做到这一点?

Laravel provides any way to do this? Laravel提供了任何方法吗?

or do i need to have any master table? 还是我需要任何主表?

or can you suggest me regular SQL query for this? 还是可以建议我对此进行常规SQL查询?

Please help. 请帮忙。 Thanks. 谢谢。

PPC Table: PPC表:

-------------------------
id            |  integer
title         |  varchar
url           |  varchar
description   |  varchar
created_at    |  timestamp
updated_at    |  timestamp
--------------------------

survey Table: 调查表:

-------------------------
id            |  integer
title         |  varchar
created_at    |  timestamp
updated_at    |  timestamp
--------------------------

find code Table: 查找代码表:

-------------------------
id            |  integer
title         |  varchar
url           |  varchar
description   |  varchar
code          |  varchar
created_at    |  timestamp
updated_at    |  timestamp
--------------------------

Make 3 queries union them with the same names for columns 使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

(

As a recommendation, it might make more sense to include all 3 of these offers in one table, and use an "offer_type" id to distinguish them from each other, instead of using 3 different tables. 作为建议,将所有这3个要约都包含在一个表中,并使用“ offer_type” ID相互区分,而不是使用3个不同的表,可能更有意义。 It might simplify your code. 它可以简化您的代码。

I believe this may work with your current setup: 我相信这可能适用于您当前的设置:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM