我知道這個問題問得不好,所以很抱歉,我有 SQL 查詢這個問題
`SELECT
c.*
FROM
merchantlink m,
company c,
merchantlinkrelation mlr
WHERE
(m.initiator_user_id = c.owner_user_id AND
m.responder_user_id = 86 AND
mlr.ptype='dealer')
OR
(m.initiator_user_id = 86 AND
m.responder_user_id = c.owner_user_id AND
mlr.ptype = 'dealer')
OR
(m.initiator_user_id = c.owner_user_id AND
c.owner_user_id=86 AND
mlr.ptype='dealer')
GROUP BY
c.id;`
我想將它轉換為 PHP laravel 查詢形式,所以嘗試了這個查詢
$twowaycompany = DB::table('company')
->join('merchantlink','merchantlink.responder_user_id', 'company.owner_user_id')
->join('merchantlinkrelation','merchantlinkrelation.merchantlink_id','merchantlink.id')
->orWhere('merchantlink.initiator_user_id', 86)
->join('merchantlink','merchantlink.initiator_user_id', 'company.owner_user_id')
->orWhere('merchantlink.responder_user_id', 86)
->pluck('name')->toArray();
但我不知道 SQL,甚至我也不明白我是如何轉換它的,有人可以幫助將 SQL 查詢轉換為 Laravel 查詢嗎?
這是 db 影像商業鏈接 
商戶鏈接關系


uj5u.com熱心網友回復:
你可以簡單地這樣做:
DB::Select(
DB::Raw('
SELECT
c.*
FROM
merchantlink m,
company c,
merchantlinkrelation mlr
WHERE
(m.initiator_user_id = c.owner_user_id AND
m.responder_user_id = 86 AND
mlr.ptype='dealer')
OR
(m.initiator_user_id = 86 AND
m.responder_user_id = c.owner_user_id AND
mlr.ptype = 'dealer')
OR
(m.initiator_user_id = c.owner_user_id AND
c.owner_user_id=86 AND
mlr.ptype='dealer')
GROUP BY
c.id
')
);
但可能有更好的方法來做到這一點。
uj5u.com熱心網友回復:
請我嘗試創建一個查詢(Laravel Query Builder)。請檢查并讓我知道它是否有效。
DB::table(DB::raw("merchantlink m, company c, merchantlinkrelation mlr"))
->select("c.*")
->whereRaw ("(m.initiator_user_id = c.owner_user_id and m.responder_user_id = 86 and mlr.ptype = 'dealer')")
->orWhereRaw("(m.initiator_user_id = 86 and m.responder_user_id = c.owner_user_id and mlr.ptype = 'dealer')")
->orWhereRaw("(m.initiator_user_id = c.owner_user_id and c.owner_user_id = 86 and mlr.ptype = 'dealer')")
->groupBy("c.id")
->get();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/321850.html
