网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 服务器与数据库 > MySQL
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 49.87.51.170
Old 2016-01-26, 11:39 AM
Galliano Galliano is offline
初级会员
 
Join Date: 2012-03-07
Posts: 1
Galliano 现在声名狼藉
Default MySQL数据库三个表的左连接查询(LEFT JOIN)

LEFT JOIN可以实现同一数据库多表联合查询符合条件的数据,以前我讲到最多的是使用LEFT JOIN实现两个表连接查询,下面看一个简单三表联合查询sql吧。
三个表联合查询我们可以使用如下语法
代码如下

select * from a left join b on a.id=b.id left join c on b.id=c.id

例子

现在要做的是:列出所有用户,以及他们所属的部门和职位。

tb_user 用户

id sName id_Pos
1 李好 2
2 好内 3
3 小外 1

tb_pos 职位

id sCaption id_Dpt
1 经理 0
2 程序 1
3 1

tb_dpt 部门

id sCaption
1 设计部
2 运维部

由于要列出tb_user中的每一条记录,这里我们需要用左连接查询。而这里要连接3个表,所以应该这样写
代码如下

SELECT u.sName p.sCaption d.sCaption FROM tb_user AS u LEFT JOIN (tb_pos AS p LEFT JOIN tb_dpt AS d ON p.id_Dpt=d.id) ON u.id_Pos=p.id;

然后运行之后就出来了tb_user新表了,方法是不是非常的简单啊,这里也是用到了left join联合查询了。

====================
调用ZEN-CART中最新10条产品评论, 同时带上产品链接就可用这方法.
Code:
$sql = "SELECT rd.reviews_text,seo.seo_url FROM reviews_description AS rd LEFT JOIN (reviews AS r LEFT JOIN seo_url AS seo ON  r.products_id=seo.products_id) ON rd.reviews_id=r.reviews_id and r.status = 1 order by r.reviews_id DESC limit 10";

$reviews_result=mysql_query($sql);

while($row = mysql_fetch_array($reviews_result)) {
					 echo  $row[reviews_text].'<br />';
					 echo  '<a href=http://www.bestpriceglasses.com/'.$row[seo_url].'>'.ucwords(str_replace("-"," ",$row[seo_url])).'</a><br /><br />';
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sql之left join、right join、inner join的区别 Ansley MySQL 2 2015-05-21 07:04 AM
Sql left join 关键字 Ansley MySQL 0 2015-05-21 06:57 AM
getting customer info from magento table structure topvip MySQL 0 2012-11-30 04:15 PM
sql 经典语句 topvip 建站交流 0 2009-02-12 07:39 AM
记者卧底23天 揭开美容业超级暴利黑幕! trademan 贸易资讯 2 2006-02-28 07:54 PM

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 03:40 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.