クエリの表示 (クエリ ビルダー 10)
View クエリは、データベース ビューに依存しない複数テーブル クエリを実現でき、データベースがビューをサポートする必要もありません。たとえば、JOIN メソッドの代替として推奨されます。 #
Db::view('User', 'id,name') ->view('Profile', 'truename,phone,email', 'Profile.user_id=User.id') ->view('Score', 'score', 'Score.user_id=Profile.id') ->where('score', '>', 80) ->select();生成された SQL ステートメントは次のようになります:
SELECT User.id,User.name,Profile.truename,Profile.phone,Profile.email,Score.score FROM think_user User INNER JOIN think_profile Profile ON Profile.user_id=User.id INNER JOIN think_socre Score ON Score.user_id=Profile.id WHERE Score.score > 80ビュー クエリでは table メソッドと join メソッドを呼び出す必要はなく、where メソッドと order メソッドを呼び出すときは、フィールドを使用するだけでよいことに注意してください。テーブル名を追加せずに名前を付けます。 INNER 結合クエリはデフォルトで使用されます。変更する必要がある場合は、次のように使用できます:
Db::view('User', 'id,name') ->view('Profile', 'truename,phone,email', 'Profile.user_id=User.id', 'LEFT') ->view('Score', 'score', 'Score.user_id=Profile.id', 'RIGHT') ->where('score', '>', 80) ->select();生成された SQL ステートメントは次のようになります:
SELECT User.id,User.name,Profile.truename,Profile.phone,Profile.email,Score.score FROM think_user User LEFT JOIN think_profile Profile ON Profile.user_id=User.id RIGHT JOIN think_socre Score ON Score.user_id=Profile.id WHERE Score.score > 80エイリアス:
Db::view('User', ['id' => 'uid', 'name' => 'account']) ->view('Profile', 'truename,phone,email', 'Profile.user_id=User.id') ->view('Score', 'score', 'Score.user_id=Profile.id') ->where('score', '>', 80) ->select();生成される SQL ステートメントは次のようになります:
SELECT User.id AS uid,User.name AS account,Profile.truename,Profile.phone,Profile.email,Score.score FROM think_user User INNER JOIN think_profile Profile ON Profile.user_id=User.id INNER JOIN think_socre Score ON Score.user_id=Profile.id WHERE Score.score > 80配列を使用してテーブル名とエイリアスを定義できます。たとえば、:
Db::view(['think_user' => 'member'], ['id' => 'uid', 'name' => 'account']) ->view('Profile', 'truename,phone,email', 'Profile.user_id=member.id') ->view('Score', 'score', 'Score.user_id=Profile.id') ->where('score', '>', 80) ->select();生成される SQL ステートメントは次のようになります。
rreeee