누군가가 도와 줄 수 있습니까?Laravel 4 복합 쿼리와 유니언/조인
사용자가 관리자에게 메시지를 보내면 batch_id가 생성되고 관리자가 사용자에게 메시지를 보내면 batch_id는 null이됩니다.
아래 테이블 구조입니다.
messages table:
---------------------------------------------------------------
id | sender_id | recipient_id | batch_id | subject | content
---------------------------------------------------------------
1 3 1 $2yQr test test
---------------------------------------------------------------
2 3 2 $2yQr test test
---------------------------------------------------------------
3 2 3 test2 test2
---------------------------------------------------------------
users table
------------------------
id | name | role
------------------------
1 admin1 | ADMIN
------------------------
2 admin2 | ADMIN
------------------------
3 user | USER
위 레코드에 있습니다. 동일한 batch_id를 갖는 메시지는 사용자가 관리자에게 보낸 메시지입니다. 빈 batch_id가있는 메시지는 admin이 사용자에게 보내는 메시지입니다.
사용자는 내가 지금까지 가지고하는 것은 예상대로
Select c.id,sender_id, recipient_id, subject, content, s.name as sender, r.name as recipient
from (Select id,sender_id, recipient_id, subject, content from messages where batch_id is null
union Select (Select b.id from messages b where b.id=a.id limit 1) as id, a.sender_id,a.recipient_id, a.subject,
a.content from messages a where a.batch_id is not null group by a.batch_id) as
c JOIN users as s ON s.id = c.sender_id JOIN users as r ON r.id = c.recipient_id where (c.sender_id =3
or c.recipient_id = 3)
작품 위의 쿼리입니다하지만 그들은 나를 쿼리 빌더 또는 웅변을 사용하려면이
id | sender_id | recipient_id | batch_id | subject | content
---------------------------------------------------------------
1 3 1 $2yQr test test
---------------------------------------------------------------
3 2 1 test2 test2
---------------------------------------------------------------
처럼 볼 수 laravel에서
위의 쿼리 또는 어쩌면 몇 가지 질문 빌더 또는 설득력있는 사람을 도울 수있는 누군가가 NE 내가 현재 새로운 해요
빌더 또는 웅변
이감사를 laravel하는 쿼리로 변환하기 쉬운 더 나은 쿼리가 있습니다!
여기에 PHP 코드가 없습니다. –
예. 쿼리를 변환하고 쿼리 작성기 또는 laravel에서 웅변을 사용하려고합니다. – blitzen12