SQL join left join query instance tutorial

Source: Internet
Author: User
Tags mysql tutorial

SQL join left join query instance tutorial

Table a: results and data

StudentID workID store
1 1 98
1 2 95
2 1 90
2 3 80

Table B: results and data
WorkID workName
1 zuoye
2 www. bKjia. c0m
3 wenti

The join left join query result is as follows:

StudentID workID workName store
1 1 zuoye 98
1 2 shiti 95
Or
StudentID workID workName store
2 1 zuoye 98
2 3 wenti 80
*/

$ SQL = 'select a. studentid, a. workid, B. workname from a join B on a. workid = B. workid where a. studentid = 1 ';

$ Sql2 = 'select. studentid,. workid, B. workname,. store from a join B on. workid = B. workid where. studentid = 1 select. studentid,. workid, B. workname,. store from a join B on. workid = B. workid where. studentid = 2 ';

// Database tutorial connection

$ Db = 'fangke _ CC ';
Mysql tutorial _ pconnect ('localhost', 'root', 'root') or die (mysql_error ());
Mysql_select_db ($ db );
Mysql_query ("set names 'gbk '");
$ Query = mysql_query ($ SQL );
While ($ rs = mysql_fetch_array ($ query ))
{
Echo $ rs [0], $ rs [1], $ rs [2];
}
/*
The result is www. bKjia. c0m.
StudentID workID workName store
1 1 zuoye 98
1 2 www. bKjia. c0m 95
*/

$ Query = mysql_query ($ sql1 );
While ($ rs = mysql_fetch_array ($ query ))
{
Echo $ rs [0], $ rs [1], $ rs [2];
}
/*
Result:
StudentID workID workName store
2 1 zuoye 98
2 3 wenti 80

Related Article

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.