MySQL 从多个表中选择多个列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8303275/
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
Select Multiple Columns From Multiple Tables
提问by Henrique Müller
I'm a beginner at MySQL and I'm having a hard time trying to figure out how to solve this problem:
我是 MySQL 的初学者,我很难弄清楚如何解决这个问题:
I have two tables with many entries each. Let's say these are the tables:
我有两个表,每个表都有很多条目。假设这些是表:
Table 1 || Table 2
------------- || -------------------
| dt1 | dt2 | || | dt3 | dt4 | dt5 |
------------- || -------------------
| 1 | abc | || | 3 | wsx | 123 |
| 7 | asd | || | 3 | qax | 456 |
| 19 | zxc | || | 4 | rfv | 789 |
------------- || -------------------
What I want to do is to have as a result one table with columns "dt2", "dt4" and "dt5" and with only one entry. For that, the query I'll apply to each table may even have to LIMIT the results. To get the results I want from each table separetelly I would do the following:
我想要做的结果是有一个包含“dt2”、“dt4”和“dt5”列并且只有一个条目的表。为此,我将应用于每个表的查询甚至可能必须限制结果。为了分别从每个表中获得我想要的结果,我将执行以下操作:
SELECT `dt2` FROM `table1` WHERE `dt1`=7;
and
和
SELECT `dt4`,`dt5` FROM `table2` WHERE `dt3`=3 LIMIT 0,1;
One more thing, I don't want to use a subquery for each column, because in the real thing I'm trying to solve, I'm calling 5 or 6 columns from each table.
还有一件事,我不想为每一列使用子查询,因为在我试图解决的实际问题中,我从每个表中调用 5 或 6 列。
Just to make clear, what I want to get is something like this:
为了清楚起见,我想得到的是这样的:
-------------------
| dt2 | dt4 | dt5 |
-------------------
| asd | qax | 456 |
-------------------
回答by Ben
SELECT a.dt2, b.dt4, b.dt5
FROM table1 a, table2 b
WHERE a.dt2 = 'asd'
LIMIT 0,1;
回答by TheTom
SELECT a.dt2, b.dt4, b.dt5
FROM table1 a, table2 b
WHERE a.dt2 = 'asd'
LIMIT 0,1;
Ben's answer is good, you can use more tables just by separating them by comma (,) , but if there's relationship between those tables then you should use some Sub Queryor JOIN
Ben 的回答很好,你可以使用更多的表,只需用逗号 (,) 分隔它们,但如果这些表之间存在关系,那么你应该使用一些子查询或JOIN
回答by JJarrard
Ben's answer solved my similar issue.
Ben 的回答解决了我的类似问题。
SELECT t1.dt2, t2.dt4, t2.dt5, t2.dt3 #get dt3 data from table2
FROM table1 t1, table2 t2
WHERE t1.dt2 = 'asd' AND t2.dt4 = 'qax' AND t2.dt5 = 456
| asd | qax | 456 | 3 |
'3' being the data I require by querying the 'qax', 456 data in table2, otherwise you're specifying exactly what data will be returned from the columns.
'3' 是我通过查询 table2 中的 'qax' 456 数据所需的数据,否则您将准确指定将从列中返回的数据。
I only had 2 tables to query in my instance, so the AND expression I can get away with using, it probably isn't best practice and there's most likely a better way for matching data from multiple tables.
我的实例中只有 2 个表要查询,因此我可以使用 AND 表达式,这可能不是最佳实践,并且很可能有更好的方法来匹配来自多个表的数据。
EDIT: I've just realised this question is 5 years old.. I hope you achieved what you wanted to by now.
编辑:我刚刚意识到这个问题已经有 5 年历史了。我希望你现在已经实现了你想要的。