Online, Association query problems, such as provincial/municipal association query, Level 1 province, level 2 City-php Tutorial

Source: Internet
Author: User
Online, Association query problems, such as provincial/municipal association query, level-1 province, level-2 city Association query problems, such as provincial/municipal association query, level-1 province, level-2 city. The results are shown in the form of Jinan Qingdao in Shandong province and Nanjing Suzhou in Jiangsu province.
I am checking the form of Jinan, Shandong, Qingdao, Nanjing, Jiangsu, and Suzhou. it is an array. How to write SQL statements for the two-dimensional array?


Reply to discussion (solution)

This is directly related to the way data is organized.
Let's talk about what your data looks like.

This is directly related to the way data is organized.
Let's talk about what your data looks like.


Databases are level-2 associations of provinces and cities.

Can't you give an example?
Different algorithms have different structures.

Can't you give an example?
Different algorithms have different structures.



Province table, and city table
To output this form: array (
"0" => array (
"Province" => "Shandong"
"Belong" => array (
Array ("city" => "Jinan "),
Array ("city" => "Qingdao "),
);
"1" => array (
"Province" => "Jiangsu"
"Belong" => array (
Array ("city" => "Suzhou "),
Array ("city" => "Nanjing "),
);
);
);
Want this form

I know what you want
But you need to know how your database is stored, whether it is a multi-table or a single table, what is the name and encoding?

I know what you want
But you need to know how your database is stored, whether it is a multi-table or a single table, what is the name and encoding?


The database is saved by province and city. province. id = city. pid. the province id is the city's pid. for the two tables, the encoding is utf8.

Create temporary table province (id int, name varchar (10) charset gbk; create temporary table city (id int, pid int, name varchar (10) charset gbk; insert into province values (1, 'Shandong '); insert into city values (1, 1, 'jinan'); insert into city values (2, 1, 'Qingdao '); insert into province values (2, 'Jiangsu '); insert into city values (3, 2, 'Suzhou'); insert into city values (4, 2, 'Nanjing '); select id, 0 as pid, name from provinceunionselect pid, id, name from cityorder by 1, 2
Id pid name 1 0 Shandong 1 1 Jinan 1 2 Qingdao 2 0 Jiangsu 2 3 Suzhou 2 4 Nanjing

It is best to paste the table structure

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.