According to the provincial, municipal and regional search content, if there is no current level, then search for higher-level content?

has a demand. We need to search for the corresponding transportation company according to origin and destination . origin and destination need to select provinces, cities, regions (counties). If there is no corresponding transportation company in the specified area, give the transportation company at the next level.

for example:
search transportation companies from Baiyun District, Guangzhou City, Guangdong Province to Xihu District, Hangzhou City, Zhejiang Province ,
if there is no match above, search for transportation companies from Baiyun District, Guangzhou City, Guangdong Province to Hangzhou City, Zhejiang Province ;
if there is no match, search Guangzhou City, Guangdong Province to Hangzhou City, Zhejiang Province transportation companies;
if there is no match above, search for Guangzhou City, Guangdong Province to Zhejiang Province transportation companies;
if there is no match above, search Guangdong Province to Zhejiang Province transportation companies;

the current idea is to put the data in a table, search according to the six conditions of the provinces, cities, autonomous regions (counties) of origin and destination , and then make several judgments until the search results are found.

is looking for a better solution, asking the bosses for advice. In terms of table building and code, mysql and php are used.

Mar.28,2021

I'm not a big shot. Let me tell you what I think.

first of all, there is a regional representation. Do you know an official code called Administrative Division Code ? This can be found in the Ministry of Civil Affairs, but there is no formatted data. You can also find a regional database that has been sorted out by others on the Internet.

We can store the code in the database or write it to a file, which is up to you.

take your example. The code of Baiyun District, Guangzhou City, Guangdong Province is 440111 , and the code of Xihu District, Hangzhou City, Zhejiang Province is 330106 .

in fact, the code is easy to understand, and every two digits represent provinces, cities, and counties.

44 is Guangdong Province, and 33 is Zhejiang Province.
01 Guangzhou in Guangdong Province and Hangzhou in Zhejiang Province.

indicates the superior area, which is followed by 0 and 6 digits.
for example, Guangdong Province is 440000 , and Guangzhou City, Guangdong Province is 440100 .

so when we save a company in a certain region, all we have to do is save the corresponding code.

for example, company table:
id name district
1 Guangdong 1 Company 440000
2 Guangdong 2 Company 440000
3 Guangdong Guangzhou Baiyun District Company 440111

in the region selection of the web page, the returned value only needs to be two digits corresponding to the price.

such as sheng=44&shi=01&xian=11

We fill in three codes
440000, 440100 and 440111

when we deal with them.

then we look up the company table directly

such as SELECT * FROM company WHERE district=440000 OR district=440100 OR district=440111;

then return the results, and we'll just check them one by one.

of course, there may be many companies, and there are too many results returned at one time. We can first query 440111, return as soon as there are results, query 440100 if not, and so on.

even, we can do this in SQL

SELECT
  CASE
    WHEN EXIST(SELECT * FROM company WHERE district=440111) THEN (SELECT * FROM company WHERE district=440111)
    WHEN EXIST(SELECT * FROM company WHERE district=440100) THEN (SELECT * FROM company WHERE district=440100)
    ELSE (SELECT * FROM company WHERE district=440000)
  END;

of course, you don't want to use the region code, you want to encode it yourself, or even save Chinese directly, and the operation is similar.

the above code entered by mobile phone has not been verified. Please search the relevant content and use it.

Menu