简体   繁体   English

从多个MySQL表中选择

[英]Selecting from multiple MySQL tables

I have a few tables that have similar fields but not exactly the same. 我有几个表有相似字段但不完全相同的表。

The same fields they have are description (text field) and modified (unixtime) 它们具有的相同字段是描述(文本字段)和修改(unixtime)

I would like to select the last modified items from these tables based on unixtime. 我想基于unixtime从这些表中选择最后修改的项目。 I cant use UNION since the tables aren't the same and the multiple table select times out. 我不能使用UNION,因为表不相同,多表选择超时。

I've been trying to look for this but no luck, either people are using JOINS or SELECT A. , B. FROM table A, table B 我一直试图寻找这个,但没有运气,人们正在使用JOINS或SELECT A. ,B. FROM表A,表B

How different they are? 他们有多么不同? Maybe you can get the common fields out: 也许你可以得到常见的领域:

select t1.name1 as name from table1
union
select t2.name2 as name from table2

Try this: 尝试这个:

SELECT
    IF (A.modified > B.modified, A.modified, B.modified) AS modified,
    IF (A.modified > B.modified, A.description, B.description) AS description,
FROM
    (SELECT description, modified FROM A ORDER BY modified DESC LIMIT 1) AS A,
    (SELECT description, modified FROM B ORDER BY modified DESC LIMIT 1) AS B
LIMIT 1

However, it's pretty much the same as just doing two queries (only more complicated) so I wouldn't recommend it. 然而,它几乎与只做两个查询(只是更复杂)相同,所以我不推荐它。

Try adding desc index on 'modified' if your select timesou, and use limit on select to return just one (last) row. 如果您选择timesou,请尝试在'modified'上添加desc index,并使用select on select返回一行(最后一行)。

Then you can: 那么你也能:

SELECT 
    A,B,C,D, desc, modified 
FROM 
   TABLEA
UNION ALL 
SELECT 
    CAST(E as <A type>), CAST(F AS <B type>) ..., desc, modified   
FROM 
   TABLE B  

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

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