mysql如何加入表 [英] Mysql how to join tables
本文介绍了mysql如何加入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个表:
- 表1包含一个主键.
- 表2所包含的外键等于表1的主键.
- 表3所包含的外键等于表2的主键.
问题:是否可以仅知道表1的主键而从表2和3中选择信息?如果是这样,请简要说明一下选择查询".我尝试进行内部联接,但是出现语法错误.
QUESTION: Is it possible to SELECT information from table 2 and 3 while only knowing the primary key of table 1? If so please give a brief example of the SELECT QUERY. I have tried doing an Inner join but got a syntax error.
推荐答案
是的.
示例:
table_a table_b table_c
_______________ _______________ _______________
| id | name | | id | gender | | id | age |
|------+--------| |------+--------| |------+--------|
| 1 | sam | | 1 | m | | 1 | 18 |
|------+--------| |------+--------| |------+--------|
| 2 | ana | | 2 | f | | 2 | 22 |
|------+--------| |------+--------| |------+--------|
为了获得以下结果:
_________________________________
| id | name | gender | age |
|------+--------+--------+--------|
| 1 | sam | m | 18 |
|------+--------+--------+--------|
| 2 | ana | f | 22 |
您可以使用以下SQL语句:
You could use the following SQL statement:
SELECT a.id, a.name, b.gender, c.age
FROM table_a AS a
LEFT JOIN table_b AS b
ON a.id = b.id
LEFT JOIN table_c AS c
ON a.id = c.id
附言:仅回答了此问题,以进行ascii art xD!
P.S.: only answered this to do the ascii art xD!
这篇关于mysql如何加入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文