简体   繁体   中英

Fetching results from different tables with the same structure

I'm being asked if I can fetch the result from 18 different Wordpress databases merged into one database with conflicting names , filter them, and then play with the results in PHP.

For example:

I have data (posts) in ara_posts, val_posts, and car_posts (all of them are tables that have post_name, post_type, post_status and post_date in their column names). I want to select all the data from those tables and filter the result (only show results that have post_type = 'post').

What I want to do with the result is compare the dates and fetch the latest 3 posts from all those 18 tables, that have their status to "publish" and the post type "post".

How is that possible?

As proposed in an answer to this question quite similar to yours , you can probably use the UNION operator. Something like this:

(SELECT post_name, post_type, post_status, post_date FROM ara_posts WHERE post_status='publish' AND post_type='post')
UNION ALL
(SELECT post_name, post_type, post_status, post_date FROM val_posts WHERE post_status='publish' AND post_type='post')
UNION ALL
(...)
ORDER BY post_date DESC
LIMIT 3

And you might just add a manual column to the select so you know from which database the data are coming:

(SELECT 'ara' as dbname, post_name, post_type, post_status, post_date FROM ara_posts WHERE post_status='publish' AND post_type='post') 
UNION ALL 
(SELECT 'val' as dbname, post_name, post_type, post_status, post_date FROM val_posts WHERE post_status='publish' AND post_type='post') 
UNION ALL (...) ORDER BY post_date DESC LIMIT 3 

I would add the ORDER BY post_date DESC LIMIT 3 part inside all subqueries. this way, all subqueries will use the index of the post_date field of the corresponding table and the main query would only have to sort 18x3 rows.

If you only have that tin the main query, it will have to fetch all (thousands? millions?) rows and then sort. I'm not sure that the optimizer is clever enough not to do that:

( SELECT post_name, post_type, post_status, post_date, 'ara' AS source 
  FROM ara_posts 
  WHERE post_status='publish' 
    AND post_type='post'
  ORDER BY post_date DESC
  LIMIT 3
)
UNION ALL
( SELECT post_name, post_type, post_status, post_date, 'val' AS source 
  FROM val_posts
  WHERE post_status='publish' 
    AND post_type='post'
  ORDER BY post_date DESC
  LIMIT 3
)
UNION ALL
(...)
ORDER BY post_date DESC
LIMIT 3

And Dennis has a very good point on how to identify the source of the final results.

I'd define a view, based on the union queries in the other answers (but without the where and limit) parts. Then you can query the view directly. A view is the analog of a function - it's your basic unit of reuse for databases.

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