SQL two tables combined query and YII under Use database query

Source: Internet
Author: User
Tags yii

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.