有人可以幫助我嗎?Laravel 4複雜的查詢與聯盟/加入
當用戶向管理員發送消息時,它將生成batch_id,然後當管理員向用戶發送消息時,batch_id將爲空。
下面是表結構。
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的消息是管理員發送給用戶的消息。
用戶應該只看到這樣的
id | sender_id | recipient_id | batch_id | subject | content
---------------------------------------------------------------
1 3 1 $2yQr test test
---------------------------------------------------------------
3 2 1 test2 test2
---------------------------------------------------------------
我有什麼到目前爲止
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)
上述作品查詢作爲預期,但他們要我用查詢生成器伶牙俐齒從laravel
有人可以幫助我使用查詢生成器或雄辯從上面的查詢或可能someo NE有一個更好的查詢,很容易轉化爲查詢生成器或雄辯
我目前新laravel
謝謝!
這裏沒有PHP代碼。 – 2014-10-02 01:47:51
是的。我試圖轉換該查詢,並使用查詢構建器或laravel雄辯。 – blitzen12 2014-10-02 01:49:30