简体   繁体   中英

How to run multiple query in single query

I don't know what kind of title i need to use for this question anyone can help me to choose a title.

Anyway My question is
I need to run this query.

SELECT key_features,about_course FROM `page_sections` WHERE page_id=15 AND country_id=241 AND  state_id=291 AND city_id=1277;

If no record found. Then i need to run same query with city_id=0

SELECT key_features,about_course  FROM `page_sections` WHERE page_id=15 AND country_id=241 AND  state_id=291 AND city_id=0;

If no record found. Then i need to run same query with state_id=0 && city_id=0

SELECT key_features,about_course  FROM `page_sections` WHERE page_id=15 AND country_id=241 AND  state_id=0 AND city_id=0;

If no record found. Then i need to run same query with country_id=0 and state_id=0 && city_id=0

SELECT key_features,about_course  FROM `page_sections` WHERE page_id=15 AND country_id=0 AND  state_id=0 AND city_id=0;   

Now there is any better way to run all this query from top to down and get only one row.

I don't want to use neatened if else...

  • If first query work no need to run second
  • else if Second query work no need to run third
  • else if third query work no need to run fouth

I simple approach would be to always select the field with the given id, or zero and then order and limit the result.

SELECT key_features,about_course
FROM `page_sections` 
WHERE page_id=15 
    AND (country_id=241 OR country_id=0)
    AND (state_id=291 OR state_id=0)
    AND (city_id=1277 OR city_id=0)
ORDER BY country_id DESC, state_id DESC, city_id DESC
LIMIT 1;

Hmmm. If you want one row, how about this:

SELECT key_features, about_course
FROM `page_sections`
WHERE page_id = 15 AND country_id = 241 AND state_id = 291 AND city_id = 1277
UNION ALL
SELECT key_features, about_course
FROM `page_sections`
WHERE page_id = 15 AND country_id = 241 AND state_id = 291 AND city_id = 0
UNION ALL
SELECT key_features, about_course
FROM `page_sections`
WHERE page_id = 15 AND country_id = 241 AND state_id = 0 AND city_id = 0
UNION ALL
SELECT key_features, about_course
FROM `page_sections`
WHERE page_id = 15 AND country_id = 0 AND state_id = 0 AND city_id = 0
ORDER BY country_id DESC, state_id DESC city_id DESC
LIMIT 1;

(This assumes that the ids are all non-negative, which seems like a reasonable assumption.)

The reason I am using UNION ALL instead of OR is so the subqueries can all take advantage of an index on page_sections(page_id, country_id, state_id, city_id) . Using OR would probably preclude the use of an index.

Assuming that there is only one (or a handful of matches) for each condition, this will be a little slower than any of your original queries. However, it will probably be faster than going back-and-forth to the database multiple times.

EDIT:

Silly me. In MySQL, this is the best way to write the query:

SELECT key_features, about_course
FROM `page_sections`
WHERE (page_id, country_id, state_id, city_id) IN
       ( (15, 241, 291, 1277),
         (15, 241, 291, 0),
         (15, 241, 0, 0),
         (15, 0, 0, 0)
       )
ORDER BY country_id DESC, state_id DESC city_id DESC
LIMIT 1;

Perhaps something like this?

SELECT 
  key_features,
  about_course 
FROM 
  page_sections 
WHERE 
  page_id = 15
ORDER BY 
  city_id DESC,
  state_id DESC,
  country_id DESC
LIMIT 1;

because of the LIMIT 1 you will only get one row. The trick is the ordering. I assume zero is the lowest number in the country_id , state_id and city_id fields, so anything higher should show up first in the results.

EDIT: I removed the selection of the specific country_id , state_id and city_id fields as you asked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM