问题描述
我是MySQL的初学者,在尝试解决该问题的过程中遇到了困难:
I'm a beginner at MySQL and I'm having a hard time trying to figure out how to solve this problem:
我有两个表,每个表都有很多条目.假设这些是表格:
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 |
------------- || -------------------
我想要做的是结果是有一张表带有"dt2","dt4"和"dt5"列,并且只有一个条目.为此,我将应用于每个表的查询甚至必须限制结果.为了分别从每个表中获得我想要的结果,我将执行以下操作:
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:
SELECT `dt2` FROM `table1` WHERE `dt1`=7;
和
SELECT `dt4`,`dt5` FROM `table2` WHERE `dt3`=3 LIMIT 0,1;
还有一件事,我不想为每个列使用子查询,因为在我要解决的真实情况下,我要从每个表中调用5或6列.
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.
只需说明一下,我想要得到的是这样的东西:
Just to make clear, what I want to get is something like this:
-------------------
| dt2 | dt4 | dt5 |
-------------------
| asd | qax | 456 |
-------------------
推荐答案
SELECT a.dt2, b.dt4, b.dt5
FROM table1 a, table2 b
WHERE a.dt2 = 'asd'
LIMIT 0,1;
这篇关于从多个表中选择多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!