Now when I query the database, I use the pg_fetch_all function to collect all the data. But the problem is that if I make a query by value, which does not exist in the table, the function will not return anything to me. For example, I make a request ... IN ('moscow', 'saint-petersburg', 'khabarovsk') . And the pg_fetch_all function will return an associative array 3 elements long. But if there is no entry in the table with the value of the saint-petersburg field, then an array of just two entries will return, where information about Khabarovsk will be located in the second place. And the sequence is important to me. That is, it is necessary that the function not only skip non-existent records, but insert an empty array instead - array() . How to do it?
|
1 answer
Not entirely sure that the entire request is correct, but the exact direction will be:
with tmp_data (id,value) as (VALUES (1,'moscow'), (2,'saint-petersburg'), (3,'khabarovsk') ) Select c.* from tmp_data z left join cities_data c on z.value = c.page_id order by tmp_data.id - Thanks, it works like a clock. - JamesJGoodwin
|
pg_fetch_all, so to speak, you have a problem not in php but in a request, you need to change it to get the desired result, if you give a request - I will try to correct - Vladimir KlykovSELECT * FROM cities_data WHERE page_id IN ('moscow', 'saint-petersburg', 'khabarovsk');simplest query -SELECT * FROM cities_data WHERE page_id IN ('moscow', 'saint-petersburg', 'khabarovsk');- JamesJGoodwintmp_datatable, not quite sure how this works in postgre, because in MySQL those versions that I use such functionality yet. - Vladimir Klykov