Sql 如何组合两个表的部分数据?

Sql 如何组合两个表的部分数据?,sql,Sql,我有两张桌子 t1 id | name | content ----------------------------------------- 1 | first | the first one 2 | second | the second one t2 id | ref_ID | description ----------------------------------------- 1 | 2 |

我有两张桌子

t1

id   |   name   |   content
-----------------------------------------
1    |  first   |   the first one
2    |  second  |   the second one


t2

id   |  ref_ID  |   description
-----------------------------------------
1    |    2     |   server
我想得到第一个表的所有列和第二个表的“description”列,其中t2.ref\u ID=t1.ID

所以结果应该是这样的

id   |   name   |      content     |   description
-----------------------------------------------------
1    |  first   | the first one    | 
2    |  second  | the second one   |  server
如果第二个表中没有条目,“说明”应为空。

我该怎么做?我试着加入,但我想那是不对的


干杯

尝试以下方式:

   select t1.*, t2.description from table1 t1 left table2 t2 on t1.id=t2.ref_id; 

请记住,应使用表的实际名称替换
表1
表2

尝试以下操作:

   select t1.*, t2.description from table1 t1 left table2 t2 on t1.id=t2.ref_id; 

请记住,您应该用表的实际名称替换
表1
表2

我在Oracle中创建了相同的表结构,具有相似的名称和相同的数据

SELECT
 t1.id, t1.cl_name, t1.cl_content, t2.cl_desc
FROM
 tbl_a t1
LEFT OUTER JOIN tbl_b t2 
ON t1.id = t2.ref_id;
结果:


我在Oracle中使用相似的名称和相同的数据创建了相同的表结构

SELECT
 t1.id, t1.cl_name, t1.cl_content, t2.cl_desc
FROM
 tbl_a t1
LEFT OUTER JOIN tbl_b t2 
ON t1.id = t2.ref_id;
结果:


提示:
左连接
。这是非常基本的SQL。您使用的是什么数据库引擎?提示:
LEFT JOIN
。这是非常基本的SQL。您使用的是什么数据库引擎?解释一下左连接是如何工作的可能会有所帮助。另外,您的答案仅适用于某些数据库引擎。该语句返回表2中具有条目的每一行。如果表2中没有与t2.ref_ID=t1.ID匹配的条目,“description”应该为空,但所有表1数据都需要在结果中。解释左连接的工作原理可能会有所帮助。另外,您的答案仅适用于某些数据库引擎。该语句返回表2中具有条目的每一行。如果表2中没有与t2.ref_ID=t1.ID匹配的条目,“description”应该为空,但所有表1数据都需要在结果中。