从三个表中选择数据? [英] Select data from three tables?

查看:49
本文介绍了从三个表中选择数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写SQL语句以从三个表中选择数据?

How can I write a SQL statement to select data from three tables?

推荐答案

使用联接

SELECT *
FROM table_1
JOIN table_2 ON (table_2.table_1_id = table_1.table_1_id)
JOIN table_3 ON (table_3.table_1_id = table_1.table_1_id)

这将要求每个表都有一个table_1_id键,并且每个表中都必须有一个条目.

This will require that each table has a table_1_id key and that there is an entry in each table.

如果table_2或table_3可能没有数据,但您仍想显示来自table_1的数据,则可以使用LEFT JOIN

You can use a LEFT JOIN if table_2 or table_3 may not have data but you still want to show the data from table_1

这篇关于从三个表中选择数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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