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...