実現したいこと
- SQLがありそれをLaravelで動かしたい
前提
下記のSQLがありこれをLaravelで動かしたい。
簡単なSQLは動くのでDBとの接続は問題なし。
INNER JOINなどのLaravelでの書き方など
まだよくわかっておりません。
宜しくお願い致します。
SELECT DISTINCT o.OrderSeq,o.OrderId, e.LoginId,e.EnterpriseNameKj, o.SiteId, s.SiteNameKj, e.SelfBillingMode,s.SelfBillingFlg,o.ClaimSendingClass FROM T_Order o INNER JOIN T_Enterprise e ON e.EnterpriseId = o.EnterpriseId INNER JOIN T_Site s ON s.SiteId = o.SiteId INNER JOIN T_ClaimHistory ch ON ch.OrderSeq = o.OrderSeq LEFT JOIN T_ClaimHistory ch2 ON ch2.OrderSeq = ch.OrderSeq AND ch2.Seq > ch.Seq AND ch2.EnterpriseBillingCode IS NULL WHERE o.ClaimSendingClass=12 AND e.SelfBillingMode IN (1,11) AND s.SelfBillingFlg=1 AND ch.EnterpriseBillingCode IS NOT NULL AND ch.ClaimPattern = 1 AND ch2.Seq IS NULL AND o.OrderSeq > 60000000 AND o.Cnl_Status = 0
試したこと
$datas = DB::table('T_Order')->distinct('T_Order.OrderSeq','T_Order.OrderId','T_Enterprise.LoginId','T_Enterprise.EnterpriseNameKj','T_Order.SiteId','T_Site.SiteNameKj','T_Enterprise.SelfBillingMode','T_Site.SelfBillingFlg','T_Order.ClaimSendingClass') ->join('T_Enterprise', 'T_Order.EnterpriseId', '=', 'T_Enterprise.EnterpriseId') ->join('T_Site', 'T_Order.SiteId', '=', 'T_Site.SiteId') ->join('T_ClaimHistory', 'T_Order.OrderSeq', '=', 'T_ClaimHistory.OrderSeq') ->leftJoin('T_ClaimHistory', function($join){ $join->on('T_ClaimHistory.OrderSeq', '=', 'T_ClaimHistory.OrderSeq', 'AND', 'T_ClaimHistory', 'T_ClaimHistory.Seq', '>', 'T_ClaimHistory.Seq', 'AND', 'T_ClaimHistory.EnterpriseBillingCode IS NULL') ->where('T_Order.ClaimSendingClass', '=', 12, 'AND', 'T_Enterprise.SelfBillingMode IN(1,11)', 'AND', 'T_Site.SelfBillingFlg', '=', 1, 'AND', 'T_ClaimHistory.EnterpriseBillingCode IS NOT NULL', 'AND', 'T_ClaimHistory.ClaimPattern', '=', 1, 'AND', 'T_ClaimHistory.Seq IS NULL', 'AND', 'T_Order.OrderSeq ', '>', 60000000, 'AND', 'T_Order.Cnl_Status', '=', 0); }) ->get();
現状
->where('T_Order.ClaimSendingClass', '=', 12, 'AND', 'T_Enterprise.SelfBillingMode IN(1,11)', 'AND', 'T_Site.SelfBillingFlg', '=', 1, 'AND', 'T_ClaimHistory.EnterpriseBillingCode IS NOT NULL', 'AND', 'T_ClaimHistory.ClaimPattern', '=', 1, 'AND', 'T_ClaimHistory.Seq IS NULL', 'AND', 'T_Order.OrderSeq ', '>', 60000000, 'AND', 'T_Order.Cnl_Status', '=', 0);
の部分にエディターで赤線がでて構文エラーになっております。
実行時エラー
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'T_ClaimHistory' (SQL: select distinct * from `T_Order` inner join `T_Enterprise` on `T_Order`.`EnterpriseId` = `T_Enterprise`.`EnterpriseId` inner join `T_Site` on `T_Order`.`SiteId` = `T_Site`.`SiteId` inner join `T_ClaimHistory` on `T_Order`.`OrderSeq` = `T_ClaimHistory`.`OrderSeq` left join `T_ClaimHistory` on `T_ClaimHistory`.`OrderSeq` = `T_ClaimHistory`.`OrderSeq` AND `T_Order`.`ClaimSendingClass` = 12) at C:\Users\USER\backlogapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669 665| // If an exception occurs when attempting to run a query, we'll format the error 666| // message to include the bindings with SQL, which will make this exception a 667| // lot more helpful to the developer instead of just the database's errors. 668| catch (Exception $e) { > 669| throw new QueryException( 670| $query, $this->prepareBindings($bindings), $e 671| ); 672| } 673| Exception trace: 1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'T_ClaimHistory'") C:\Users\USER\backlogapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php:331 2 PDO::prepare("select distinct * from `T_Order` inner join `T_Enterprise` on `T_Order`.`EnterpriseId` = `T_Enterprise`.`EnterpriseId` inner join `T_Site` on `T_Order`.`SiteId` = `T_Site`.`SiteId` inner join `T_ClaimHistory` on `T_Order`.`OrderSeq` = `T_ClaimHistory`.`OrderSeq` left join `T_ClaimHistory` on `T_ClaimHistory`.`OrderSeq` = `T_ClaimHistory`.`OrderSeq` AND `T_Order`.`ClaimSendingClass` = ?") C:\Users\USER\backlogapi\vendor\laravel\framework\src\Illuminate\Database\Connection.php:331 Please use the argument -v to see more details.
回答1件
あなたの回答
tips
プレビュー