简体   繁体   English

从具有相同结构的不同表中获取结果

[英]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.有人问我是否可以从合并到一个名称冲突的数据库中的 18 个不同 Wordpress 数据库中获取结果,过滤它们,然后在 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).我在 ara_posts、val_posts 和 car_posts 中有数据(帖子)(它们都是列名中包含 post_name、post_type、post_status 和 post_date 的表)。 I want to select all the data from those tables and filter the result (only show results that have post_type = 'post').我想 select 这些表中的所有数据并过滤结果(仅显示具有 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".我想要对结果做的是比较日期并从所有这 18 个表中获取最新的 3 个帖子,这些表的状态为“发布”,帖子类型为“帖子”。

How is that possible?这怎么可能?

As proposed in an answer to this question quite similar to yours , you can probably use the UNION operator.正如在与您的问题非常相似的答案中提出的那样,您可能可以使用UNION运算符。 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 中添加一个手动列,以便了解数据来自哪个数据库:

(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.我会在所有子查询中添加ORDER BY post_date DESC LIMIT 3部分。 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.这样,所有子查询都将使用相应表的post_date字段的索引,而主查询只需对 18x3 行进行排序。

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.我会根据其他答案(但没有 where 和 limit)部分中的联合查询来定义一个视图。 Then you can query the view directly.然后就可以直接查询视图了。 A view is the analog of a function - it's your basic unit of reuse for databases.视图类似于 function - 它是数据库重用的基本单元。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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