A combined query of SQL two tables uses join on
For example: Two table queries:
Select U.username, T.title
where User U is used to simplify the table name join is to join the other table on is the condition that represents the query U.username is the username field in the user table
The same principle of multi-table query is the same
Here is the SQL statement:
SELECT * from user u join task t in u.id = T.idjoin task_user tu on tu.id = t.id
here's how to use query queries under the YII framework:
$query = new query (); $query->select (' title,content,task.enable,task.id '); $query->from (' Task '); $query->leftjoin (' Task_user_rel ', ' task.id= task_user_rel.task_id '); $query->leftjoin (' user ', ' task_user_rel.user_id = User.ID '); $query->leftjoin (' Org_user_rel ', ' user.id = org_user_rel.user_id '); $query->leftjoin (' org ', ' org_user_rel.org_id =org.id '); $query->where ([ ' org.id ' = = $orgModel->id ]) $data = $query->all (); $data = $query->one ();
In fact, under the YII framework there are many ways to query the database, such as:
$data = Jdcrmsmsusermodel::find ()->where ([ ' vender_id ' = [ $venderId ] ])->all ();
or use Findbysql (), FindOne (), FindAll (), and so on, instead of using Find () to query.
Combination queries for SQL two tables and using database queries under Yii