2017-11-15 19 views
7

以下のSQL文のDBクエリバージョンを作成するのに手伝ってください。 私はselect文と分割された結合について少し助けが必要です。未処理のSQLからlaravelクエリ

私はこれまでこれを行うことができました。

$query = DB::table(raw('SapakInAdminOrder a')) 
->select(raw('a.*')) 
->leftJoin(raw('currency cu'), 'a.currency', '=', 'cu.id') 
->leftJoin(raw('moodboards m'), 'a.orderMoodboardID', '=', 'm.id') 
->join(raw('clients b'), 'a.clientID', '=', 'b.id') 
->leftJoin(raw('moodboards mc'), 'b.moodboardID', 'mc.id') 
->join(raw('sapakim c'), 'b.sapakID', '=', 'c.id') 
->leftJoin(raw('sapakim sm'), 'c.managerid', '=', 'sm.id') 
->leftJoin(raw('products p'), 'a.productKey', '=', 'p.id') 
->where(function ($query) { 
    $query->whereNull('a.isDeleted'); 
    $query->orWhere('a.isDeleted', '!=', 1); 
}); 

しかし、これを達成する必要があります。

select * from (select ROW_NUMBER() OVER(ORDER BY case when (indesign.status=4 or indesign.statusdate is null) then getdate()+2 else indesign.statusdate end ASC) AS RowNum,a.* 
FROM sapakInAdminOrder a 
left join currency cu on cu.id=a.currency 
left join moodboards m on m.id=a.orderMoodboardID 
inner join Clients b on a.clientID=b.id 
left join moodboards mc on mc.id=b.moodboardID 
inner join Sapakim c on b.sapakID=c.id 
left join Sapakim sm on sm.id=c.managerid 
left join products p on p.id=a.productKey 
left join (select * from (select ROW_NUMBER() over(PARTITION BY orderID ORDER BY id DESC) r, * from orderCommunication) f where r=1) chat on chat.orderId = a.id 
left join (select id,[status],orderid,approveSMSDate,coverImage,statusDate from (SELECT id,[status],statusDate,approveSMSDate,coverImage,orderid,ROW_NUMBER() OVER(PARTITION BY orderid ORDER BY id DESC) AS r FROM SapakimInAdminDesigns) f where r=1) indesign on a.id=indesign.orderid 
where (a.isDeleted is null or a.isDeleted != 1) and 
     c.inAdminManagerID=(select id from sapakim where sapakguid='test') and 
     c.sapakguid='test' and 
     a.isFreeDesign=0 and 
     a.transactionID = -1 and 
     (a.designerPaid is null or a.designerPaid=0) and 
     (chat.sentToPrinter is null and chat.sentToManager is null and chat.sentToDesigner is null) 
) bb where RowNum>=1 and RowNum<31 
ORDER BY RowNum asc 

私はシンプルなものを行うことができますが、かなり本当に参加し、select文分割まわりで私の頭をラップすることができませんでした。

本当にありがとうございます。

ありがとうございます。

+0

テストスキーマをセットアップし、予期した結果と一緒にデータをシードするためにSQLを共有すると便利です –

答えて

0

私もこのタイプの問題に悩まされていますが、私は解決策を得て、それは私のためにうまくいきます!

使用DB ::準備ができていない()クエリのこのタイプの

$path = base_path() . "/storage/agency.sql"; 
$sql = file_get_contents($path); 
DB::unprepared(DB::raw($sql)); 

すべてのSQLコマンドLaravelでは、デフォルトで用意されているが、時にはあなたが準備されていないモードでコマンドを実行する必要がありますこれは、データベースの中には準備モードで実行できないものがあるためです。

+0

DB :: query()を使用して簡単にクエリできますが、これは洗練された解決策ではありません。 –

+0

DB :: unpreparedは生のクエリを実行しますが、DB :: rawは必須ではありません。テキストファイル(特に、ストレージフォルダ内のファイル)からクエリを読み込むと、ファイルが侵害された場合に問題が発生するように求められます。リポジトリ・パターンを使用し、ソース・コードでDB :: select()を使用する方が良いでしょう。 –

0

これはあなたの意志ですか?

$result = DB::Select("select * from (select ROW_NUMBER() OVER(ORDER BY case when (indesign.status=4 or indesign.statusdate is null) then getdate()+2 else indesign.statusdate end ASC) AS RowNum,a.* 
FROM sapakInAdminOrder a 
left join currency cu on cu.id=a.currency 
left join moodboards m on m.id=a.orderMoodboardID 
inner join Clients b on a.clientID=b.id 
left join moodboards mc on mc.id=b.moodboardID 
inner join Sapakim c on b.sapakID=c.id 
left join Sapakim sm on sm.id=c.managerid 
left join products p on p.id=a.productKey 
left join (select * from (select ROW_NUMBER() over(PARTITION BY orderID ORDER BY id DESC) r, * from orderCommunication) f where r=1) chat on chat.orderId = a.id 
left join (select id,[status],orderid,approveSMSDate,coverImage,statusDate from (SELECT id,[status],statusDate,approveSMSDate,coverImage,orderid,ROW_NUMBER() OVER(PARTITION BY orderid ORDER BY id DESC) AS r FROM SapakimInAdminDesigns) f where r=1) indesign on a.id=indesign.orderid 
where (a.isDeleted is null or a.isDeleted != 1) and 
     c.inAdminManagerID=(select id from sapakim where sapakguid='test') and 
     c.sapakguid='test' and 
     a.isFreeDesign=0 and 
     a.transactionID = -1 and 
     (a.designerPaid is null or a.designerPaid=0) and 
     (chat.sentToPrinter is null and chat.sentToManager is null and chat.sentToDesigner is null) 
) bb where RowNum>=1 and RowNum<31 
ORDER BY RowNum asc"); 
+0

それはDB :: query()ですが、それでも私は –

0

これらのパーティションクエリのデータベースビューを作成する必要がありますか? その後、データベースからビューに参加できます。

技術的には、これらの分析機能は、通常、フレームワークによってサポートされていません。

+0

という意味ではありません。つまり、laravelでこれを行うことはできません。 –

+0

すべてのDB-Sが分析機能をサポートしているわけではありません。 ORM-sは普遍的であり、すべての可能なDBエンジンをサポートするように作られています。 ORMでこれらの洗練されたクエリを実装する必要はなく、ほとんどの人はその存在を知らない。たとえばJOOQのようないくつかの例外がありますが、これはPHP言語ではありません(https://blog.jooq.org/2013/11/03/probably-the-coolest-sql-feature-window-functions/)。 – Aret

0

この新しいLaravelツールOratorをチェックしてください。 「レガシー」SQLを貼り付けるだけで、LaravelのDB Query Builderの構文を返します。あなたが識別呼ば一対一の関係があるモデルのユーザーでは、関係が雄弁モデルウィットフロントエンド用途に https://laravel.com/docs/master/eloquent-relationships#one-to-one

これを表示したいと仮定すると、

Laravel Orator News Article

Online Conversion Tool

0

メソッドphone()によって、あなたが$ user変数を渡したと仮定しているshowビューに行くと、次のようになります。

<?php dd($user->phone); ?> 

あなたがこれを意味するのではない場合、私は質問の全体の点を見逃しているかもしれません。

関連する問題