MYSQL查询选择两个表

时间:2016-02-07 16:57:25

标签: mysql

我在从两个不同的表中选择数据时遇到问题。见例子

TABLE1
UserID | Name     | Description
myID01 | myname   | mydescription
myID02 | myname2  | mydescription2
myID03 | myname3  | mydescription3

TABLE2
ID | UserID  | Picture        | Pic_Description
1  | myID01  | mypicture.jpg  | This is my picture

输出必须显示TABLE1中的所有记录,并仅选择TABLE1到TABLE2中匹配的UserID以显示图片。

显示我预期输出的正确MYSQL语句必须是什么?谢谢。

2 个答案:

答案 0 :(得分:1)

您可以使用join加入多个表格。

select t1.UserID as userid, t1.Name as name, t1.Description as description, t2.picture as picture
from table1 as t1
join table2 as t2
on t1.UserID = t2.UserID

答案 1 :(得分:0)

您可以使用此

SELECT * -- You can change to your desired column
FROM TABLE1 t1
LEFT JOIN TABLE2 t2 ON t2.UserId = t1.UserId