A mysql row corresponds to a multi-column Association query problem. There are examples and detailed descriptions, and there are two tables.
Table t1 has four field IDs: city_id1, city_id2, and city_id3.
Table t2 has two field IDs, city_name
City_id1, 2, and 3 in table t1 correspond to IDS in table t2.
For example:
Table t1 has one record
Table t2 has three Records
26 Beijing
29 Shanghai
50 Tianjin
Now, I want to find this record in t1 in an SQL statement and display the city_id1, 2, and 3 cities. for example
Results: 1. Beijing, Shanghai, and Tianjin
How should I write SQL statements?
Reply to discussion (solution)
select id, (select city_name from t2 where id=t1.city_id1) as city_name1, (select city_name from t2 where id=t1.city_id2) as city_name2, (select city_name from t2 where id=t1.city_id3) as city_name3 from t1
I am talking about another solution ~
Write a function that defines an array of static variables $ city and uses the city id as the key. the city name is called a value.
Each time you edit the city data in the background, a file that serializes the city data is generated.
When the system starts, the static variable reads the file.
This function is used to input the city id and returns the city name immediately based on $ city.
Because it is a static variable, the overhead of multiple calls in a single request is not large, that is, the initialization needs to read a file.
In this way, you can change the name of all the cities on your website.
I am talking about another solution ~
Write a function that defines an array of static variables $ city and uses the city id as the key. the city name is called a value.
Each time you edit the city data in the background, a file that serializes the city data is generated.
When the system starts, the static variable reads the file.
This function is used to input the city id and returns the city name immediately based on $ city.
Because it is a static variable, the overhead of multiple calls in a single request is not large, that is, the initialization needs to read a file.
In this way, you can change the name of all the cities on your website.
In fact, every time I find a city array like this, I will pass in city_id to get the city name.
The method you mentioned is equivalent to caching this array.
The big xu method can be implemented, but it seems that the efficiency is not very high. I thought mysql could efficiently process this matching relationship ~~~ It seems that cache can only be used first.
In fact, every time I find a city array like this, I will pass in city_id to get the city name.
The method you mentioned is equivalent to caching this array.
The big xu method can be implemented, but it seems that the efficiency is not very high. I thought mysql could efficiently process this matching relationship ~~~ It seems that cache can only be used first.
If there are too many cities, the cache will not feel like a solution.
Each request must read, deserialize, and load the file into the memory.
No practical tests have been conducted on the strengths of this method compared with SQL.
There was another idea in the twinkling of an eye
When you need to print the CITY name, first write it in the form of {CITY: 123}
In ob_get_clean at the bottom of the page, use regular expressions to dig out these items, remove duplicate IDs, query the database based on this group of unique IDs, and replace the names
This method may consume a little more CPU, but does not need to read the hard disk. database operations on the city are also completed once.
Both methods have been tried. at last, the efficiency of the old xu method is acceptable. it may be because the table is small.