한계가있는 3 개의 테이블에서 데이터 선택
문제
3 개의 테이블 ppc_offers, survery_offers, find_offers (관리 섹션 에서이 테이블에 데이터를 입력하십시오)
이제 프런트 엔드 에서이 테이블의 최신 3 개의 제안을 표시하고 싶습니다.
i.e.최근 3 일이 올 것입니다. 단일 테이블이나 여러 테이블에서 사용할 수 있습니다 ... 그것은 단지 "최신 3"
조건이 있습니다.어떻게 할 수 있습니까?
LARAVLAVE는이 작업을 수행 할 수있는 방법을 제공합니까?
또는 마스터 테이블을 가질 필요가 있습니까?
또는 정기적 인 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 가지 제안을 포함시키는 것이 더 이상 의미가 있으며 "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();
. 다른 팁
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
.
(
)제휴하지 않습니다 StackOverflow