The province city SQL question two tables are associated with the two tables with the same field select query. The two identical fields cannot be displayed in the real thinkphp query. selectthinkphp

Source: Internet
Author: User

The province city SQL question two tables are associated with the two tables with the same field select query. The two identical fields cannot be displayed in the real thinkphp query. selectthinkphp

1. Write an SQL statement to associate two tables. The following fields must be displayed: city id city name = name province name = name

Select c. id, c. name, p. name from city as c join province as p on c. pid = p. id;

Result:

2. Use thinkphp to associate two tables. The following fields are displayed: city id city name = name province name = name

$ Model = new \ Think \ Model ();
$ Result = $ model-> query ("select c. id, c. name, p. name from city as c join province as p on c. pid = p. id ");
Var_dump ($ result );

Output result:

Array (3 ){
[0] =>
Array (2 ){
["Id"] =>
String (1) "1"
["Name"] =>
String (6) "Hunan"
}
[1] =>
Array (2 ){
["Id"] =>
String (1) "2"
["Name"] =>
String (6) "Hubei"
}
[2] =>
Array (2 ){
["Id"] =>
String (1) "3"
["Name"] =>
String (6) "Guangdong"
}
}

The queried data is incorrect. Is the SQL statement incorrect? Yes, because the two tables have two identical name fields, the solution is to modify the name field of a table. For example, modify the name field of the city table to vname.

$ Model = new \ Think \ Model ();
$ Result = $ model-> query ("select c. id, c. vname, p. name from city as c join province as p on c. pid = p. id ");
Var_dump ($ result );

Running result:

Array (3 ){
[0] =>
Array (3 ){
["Id"] =>
String (1) "1"
["Vname"] =>
String (6) "Changsha"
["Name"] =>
String (6) "Hunan"
}
[1] =>
Array (3 ){
["Id"] =>
String (1) "2"
["Vname"] =>
String (6) "Wuhan"
["Name"] =>
String (6) "Hubei"
}
[2] =>
Array (3 ){
["Id"] =>
String (1) "3"
["Vname"] =>
String (6) "Guangzhou"
["Name"] =>
String (6) "Guangdong"
}
}

Normally displayed, as required by the question! Is there any other way to modify the field name? Ask a friend, saying that only the field name can be modified...

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.