Online, related query problems, such as provincial and municipal related queries, first-class province, two-tier city

Source: Internet
Author: User
Related query problems, such as provincial and municipal related queries, first-class provinces, two-tier city. The results are as follows: Jinan, Qingdao, Shandong, Jiangsu Province, Nanjing, Suzhou, this form.
I am now looking for Shandong Jinan, Shandong Qingdao, Jiangsu, Nanjing, Jiangsu Suzhou This form, is an array. Want to find out what kind of two-dimensional array to write SQL


Reply to discussion (solution)

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

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


The database is a two-level association of Provinces and cities.

Can't you give me an example?
Different algorithms for structure are different

Can't you give me an example?
Different algorithms for structure are different



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 you want it this way.
But you need to know how you put it in the library, whether it's a multi-table or a single-table, what the name and code are.

I know you want it this way.
But you need to know how you put it in the library, whether it's a multi-table or a single-table, what the name and code are.


The library is according to the province and the city, province.id = City.pid, the province's ID is the city's PID, the two tables, the code is UTF8

Create temporary table province (id int, name varchar) CharSet gbk;create temporary table city (id int, pid int, name varchar (+) charset Gbk;insert into province values (1, ' Shandong '); insert into city values (1, 1, ' Jinan '); insert INTO city value S (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
ID PID name 1  0   Shandong 1  1   jinan 1  2   Qingdao 2  0   Jiangsu 2  3   Suzhou 2  4   

Best to post a 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.