从两个表中选择Mysql数据

16

有没有人能告诉我如何从两个表中选择数据,而无需使用连接操作?

像这样:

SELECT t1.*, 
       t2.*
  FROM table1 t1, 
       table2 t2

###澄清 我有两个表,它们具有相同的字段。例如:table1包含2011年的数据,而table2包含2012年的数据。我想获取所有这些数据。

###进一步澄清: 所需的结果集可以通过以下方式生成:

(SELECT tr.full_name, tr.headlines, tr.content, tr.stamp, tr.person_key
 FROM tbl_transactions tr
          JOIN persons p ON p.person_key = tr.person_key
          JOIN teams t ON (pp.membership_id = t.id and pp.membership_type = 'teams')
 WHERE t.team_key = '')
UNION
(SELECT tr.full_name, tr.headlines, tr.content, tr.stamp, tr.person_key
 FROM tbl_transactions_bk_2012 tr
          JOIN persons p ON p.person_key = tr.person_key
          JOIN teams t ON (pp.membership_id = t.id and pp.membership_type = 'teams')
 WHERE t.team_key = ''

这位用户希望了解是否有其他方法可以加快查询速度("我尝试在这些查询之间使用UNION,但查询速度为0.1887秒,有点慢。")。

(@Jetoox: 如果这不是你的意图,请编辑你的问题并澄清。)


1
你有特定的使用场景还是只是好奇呢? - Ignacio Vazquez-Abrams
我有两张表,它们具有相同的字段。例如,table1 包含了2011年的数据,而 table2 包含了2012年的数据。我想要获取它们所有的数据。 - Jetoox
如果列数相同,则可以在此处使用Union。 - joshua
可能是重复的问题:MySQL - 从具有相同结构但不同数据的多个表中选择数据 - Ignacio Vazquez-Abrams
这两个表格都包含相同的人员关键字例子,但当我运行查询时,MySQL显示未知列person_key。 - Jetoox
显示剩余7条评论
5个回答

29
只需将JOIN条件放在WHERE子句中即可:
SELECT t1.*, t2.*
FROM table1 t1, table2 t2
WHERE t1.id = t2.t1_id

这是一个内连接。

更新

在查看您的查询时:在这种特殊情况下,tbl_transactionstbl_transactions_bk_2012之间没有关系(即在person_key上进行连接是毫无意义的,因为这两个表之间不存在像tbl_transactions和persons之间那样的关系)。

然后,您应该使用UNION方法。试图使用JOINFROM xx, yy WHERE xx.id=yy.id将第一个查询与第二个查询连接起来是没有意义的,并且不会给您需要的结果。

顺便说一句,在以后的提问中,请将您当前的查询/尝试放在您的帖子中 - 正如您可以看到的,这将防止您获得与您的问题不相关的答案(就像我的第一次尝试一样)。


这两个表都包含相同的示例person_key,但当我运行查询时,MySQL会显示未知列person_key。 - Jetoox
请发布您正在使用的确切查询,以及show columns from table1 where Field='person_key'show columns from table2 where Field='person_key'的输出。如果您的两个表通过一个公共person_key相关,请使用WHERE条件。如果您的两个表完全不相关,请使用UNION - mathematical.coffee
SELECT tr.full_name, tr.headlines, tr.content, tr.stamp, tr.person_key FROM tbl_transactions tr JOIN persons p ON p.person_key = tr.person_key JOIN teams t ON (pp.membership_id = t.id and pp.membership_type = 'teams') WHERE t.team_key = '' UNION SELECT tr.full_name, tr.headlines, tr.content, tr.stamp, tr.person_key FROM tbl_transactions_bk_2012 tr JOIN persons p ON p.person_key = tr.person_key JOIN teams t ON (pp.membership_id = t.id and pp.membership_type = 'teams') WHERE t.team_key = '' - Jetoox
在这种特殊情况下,由于tbl_transactionstbl_transactions_bk_2012之间没有关联(即在person_key上连接这些表是无意义的,因为这两个表之间的关系不像tbl_transactionspersons那样有关联),因此您应该使用“UNION”方法 - 尝试使用“JOIN”或“FROM xx,yy WHERE xx.id = yy.id”将第一个查询连接到第二个查询是没有意义的,也无法给您所需的结果。 - mathematical.coffee

12

你想要 UNION

(SELECT tr.full_name, tr.headlines, tr.content, tr.stamp, tr.person_key
 FROM tbl_transactions tr
          JOIN persons p ON p.person_key = tr.person_key
          JOIN teams t ON (pp.membership_id = t.id and pp.membership_type = 'teams')
 WHERE t.team_key = '')
UNION
(SELECT tr.full_name, tr.headlines, tr.content, tr.stamp, tr.person_key
 FROM tbl_transactions_bk_2012 tr
          JOIN persons p ON p.person_key = tr.person_key
          JOIN teams t ON (pp.membership_id = t.id and pp.membership_type = 'teams')
 WHERE t.team_key = ''

关于 Union 的另一个问题的答案:https://dev59.com/IXRC5IYBdhLWcg3wG9Nb#409720 - Emile Bergeron
这两个表都包含相同的示例person_key,但当我运行查询时,MySQL会显示未知列person_key。 - Jetoox

4
select t1.*, t2.* from table1 t1, table2 t2
where t1.fkey = t2.pkey

这两个表都包含相同的示例person_key,但当我运行查询时,MySQL会显示未知列person_key。 - Jetoox

2
select t1.* , t2.*
from t1, t2 where t1.id=t2.id;

这两个表都包含相同的示例person_key,但当我运行查询时,MySQL会显示未知列person_key。 - Jetoox

0

在使用 UNION 查询时,您可能还想为您用于连接和过滤的任何列添加索引以提高性能


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接