MySQL left connection SQL left-hand join statement detailed

Source: Internet
Author: User
Tags character set mysql tutorial

MySQL tutorial left-connect SQL left-hand join statement detailed
MySQL left-join query is a way of federated query, that is, two related tables are joined together in this way to query, which makes it easier to call data and avoid multiple loops nesting.


Left JOIN keyword LEFT join for query
, the written explanations are as follows:

Left OUTER join (left connection): The result set includes both matching rows of the join table and all rows of the left join table.
Let me explain, that is to say, the return result of the left-linked query contains all the data on the left table of the left-hand join keyword, as well as the eligible resources in the right table that matches the left table. Simply put, the left table as the center, with the right table in accordance with the conditions of resources.


Syntax for SQL statements:

Sql:select a.a, A.B, A.C, B.C, B.D, B.f from a left-out join B on a.a = B.C

I believe you will be very dizzy after reading, it does not matter, I explain that we all understand, seemingly complex, in fact, organized very clearly. What it means is:

One
Select A.A, A.B
In
A.A is the meaning of a field in Table A.

Two
Between select and from Is the field in table B of table a related to query.

Three
From a left out join B
is from table A as the main left link on the right table B,
The match condition is the a field of a table equals the C field of table B.

Through these explanations I believe that we all understand that 80%, the following we through the example of the demonstration again to say:

First set up two tables

Table pic
Table to store the name of the picture

CREATE table ' image '. ' Pic ' (

' id ' int (ten) NOT NULL Auto_increment primary key,
' pname ' varchar character set UTF8 collate utf8_unicode_ci NOT NULL

) engine = InnoDB


Table II for storing picture comments

CREATE table ' image '. ' Comment ' (

' id ' int (ten) NOT NULL Auto_increment primary key,
' PID ' int (a) NOT NULL,
' content ' varchar character Set UTF8 collate utf8_unicode_ci NOT NULL

) engine = InnoDB


Next I want to combine the two tables through a SQL statement, note that before we query two tables must be the first to write a table of SQL and then in accordance with the criteria loop query another table, now do not, through this query, the return results into an array,


SQL statement wording: SELECT
pic.*,comment.*
From
Pic Left JOIN Comment
On
Pic.id=comment.pid

The code is as follows:


$conn = mysql_connect ("localhost", "root", "");


if (! $conn) {


Die (' Connect MySQL database tutorial failed: '. Mysql_error () ');

}


mysql_select_db (' image ');

$sql = "Select
pic.*,comment.*
From
Pic Left JOIN Comment
On
Pic.id=comment.pid ";

$re =mysql_query ($sql);

while ($re 1=mysql_fetch_array ($re))

{

$arr []= $re 1;


}

Print_r ($arr);


Print the results as follows:

Array

(


[0] => Array


(


[0] => 1


[ID] => 1


[1] => AAAAAAAAA


[PName] => aaaaaaaaa


[2] => 1


[3] => 1


[PID] => 1


[4] => CCCCCCC


[Content] => CCCCCCC


)

[1] => array


(


[0] => 2


[ID] => 2


[1] => bbbbbbbbbbb


[PName] => bbbbbbbbbbb


[2] => 2


[3] => 2


[PID] => 2


[4] => VVVVVVV

[Content] => VVVVVVV


)


)

Ok

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.