Mysql多表查询

有2个表,一个表里面必须另外一个表的uuid(主键),sql语句写的时候,

要写成:select  t.name,s.* from `jxc_contacts` t,`jxc_customer` s where  t.target_id=s.uuid (where  t.target_id=s.uuid这个条件的前提就是在 t表中,必须要有s表的uuid,不然查询不到,t.target_id=s.uuid,t里面有他自己的uuid,t里面有target_id,这个target_id是s里面的uuid,其实意思就是,target_id的值,必须要和s表里面的Uuid要一样)

刚刚又更新了,我现在是3张表查询,但是我有2张表都有同样的Name,然后可以在 t.name后面再给他一个别名tname,要记得加上一个as 这样,他就不会重复了。

select  t.name as tname,s.*,f.name as fname from `jxc_contacts` t,`jxc_customer` s,jxc_attachment f where  t.target_id=s.uuid and f.`target_id`=s.`uuid` 

 

 

 

 

posted @ 2017-01-12 10:46  李科纪  阅读(159)  评论(0编辑  收藏  举报