如何一次插入两个相关表中的数据 [英] how can I insert data from two related tables at once

查看:60
本文介绍了如何一次插入两个相关表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have two tables in SQL . one has 2000 records and another has 20. I would like to relate these two tables into one.

推荐答案

你需要使用JOIN,假设两个表有一个共同的字段,例如id。这是非常基本的SQL。



例如:

You need to use a JOIN, assuming the two tables have a field in common, such as an id. This is very basic SQL.

For example:
SELECT t1.id, t1.field1, t1.field2, t2.field1, t2.field2
FROM table1 t1
LEFT JOIN table2 ts ON t1.id = t2.id





这将获得所有记录。如果你想进入一个新表,我建议先创建表,然后你可以使用上面的select语句进行插入。



That will get all the records. If you want to go into a new table I suggest creating the table first and then you could just do an insert using the select statement above.


这篇关于如何一次插入两个相关表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆