쿼리 보기(쿼리 빌더 10)


뷰 쿼리는 데이터베이스 뷰에 의존하지 않고 뷰에 대한 데이터베이스 지원이 필요하지 않은 다중 테이블 쿼리를 구현할 수 있습니다. 이는 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

view query에 주의하세요. 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 문은 다음과 같습니다.

SELECT member.id AS uid,member.name AS account,Profile.truename,Profile.phone,Profile.email,Score.score FROM think_user member INNER JOIN think_profile Profile ON Profile.user_id=member.id INNER JOIN think_socre Score ON Score.user_id=Profile.id WHERE Score.score > 80