MySQL Mysql从两个表中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9460435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysql selecting from two tables
提问by Jetoox
Can anybody tell me how to select data from two tables, without having to use join?
谁能告诉我如何从两个表中选择数据,而不必使用连接?
Something like this:
像这样的东西:
SELECT t1.*,
t2.*
FROM table1 t1,
table2 t2
Clarification
澄清
I have these two tables, that have the same fields. IE: table1
contains data from 2011 and table2
contains data in 2012. I want to get them all.
我有这两个表,它们具有相同的字段。IE:table1
包含 2011 年的数据,table2
包含 2012年的数据。我想全部获取。
Further clarification:
进一步澄清:
The result set desired can be produced by:
可以通过以下方式生成所需的结果集:
(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 = '')
and the OP wishes to see if there are alternative ways to speed this up ("I tried to use UNION
in between those queries. but query speed took 0.1887 secs. it's kinda slow.")
并且 OP 希望看看是否有其他方法可以加快速度(“我试图UNION
在这些查询之间使用。但查询速度需要 0.1887 秒。它有点慢。”)
(@Jetoox: if this is not your intent, please edit your question and clarify).
(@Jetoox:如果这不是您的意图,请编辑您的问题并澄清)。
回答by mathematical.coffee
Just put the join condition in the WHERE clause:
只需将连接条件放在 WHERE 子句中:
SELECT t1.*, t2.*
FROM table1 t1, table2 t2
WHERE t1.id = t2.t1_id
That is an inner join, though.
不过,这是一个内部连接。
UPDATE
更新
Upon looking at your queries: In this particular case, there is no relation between tbl_transactions
and tbl_transactions_bk_2012
(i.e. joining these on person_key is meaningless because there is no relationship between the two tables in the way that (say) tbl_transactions and persons are related).
在查看您的查询时:在这种特殊情况下,tbl_transactions
和之间没有关系tbl_transactions_bk_2012
(即在 person_key 上加入这些是没有意义的,因为两个表之间没有关系(比如)tbl_transactions 和persons 相关)。
Then, you should use the UNION
approach. Trying to join the first query to the second using either JOIN
or FROM xx, yy WHERE xx.id=yy.id
is meaningless and won't give you the results you need.
然后,您应该使用该UNION
方法。尝试使用JOIN
or将第一个查询连接到第二个查询FROM xx, yy WHERE xx.id=yy.id
是没有意义的,并且不会为您提供所需的结果。
By the way, in the future, put your current query/attempt in your post - as you can see it will prevent you from getting answers that aren't appropriate for your question (as my first attempt was).
顺便说一句,将来,将您当前的查询/尝试放在您的帖子中 - 正如您所看到的,它会阻止您获得不适合您的问题的答案(就像我的第一次尝试一样)。
回答by silly
select t1.*, t2.* from table1 t1, table2 t2
where t1.fkey = t2.pkey
回答by jyotiprakash
select t1.* , t2.*
from t1, t2 where t1.id=t2.id;
回答by Stephen Senkomago Musoke
While using the UNION query, you may also want to add indexes to any columns that you are using to join and filter which will improve performance
在使用 UNION 查询时,您可能还想向用于连接和过滤的任何列添加索引,这将提高性能