请教一个mysql三表关联查询的问题
天蓬老师
天蓬老师 2017-04-17 11:56:25
[MySQL讨论组]

orderlogistics(物流信息表)
orderlogistics_id:自增id
userid:用户id,
ordercode:订单号
logistics:物流公司,字符型
logisticsid:物流号
yunfei
status

customer(客户表)
customer_id:自增id
userid:用户id,
username
nicename
pro,city,dist,addr:收货地址,这里是4个字段,都是字符型,直接写入的省市区县地址的中文
mobile

order(订单表)
order_id:自增id
productname:商品名称
inprice
num
unit
incurrency
other
exchange_rate
cost
outprice
profit
ordercode:订单号
userid:用户id,
customerid:自增id
orderdate
status

有以上三个表,需要列出订单列表,订单列表所用到的字段如下:order_id ,username,nicename,mobile,pro,city,dist,addr,logistics,logisticsid,status,cost,outprice,other,profit

请问该如何写sql查询,sql不精,让各位见笑了。

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
大家讲道理

select a.order_id ,a.cost,a.outprice,a.other,a.profit,a.status,b.username,b.nicename,b.mobile,b.pro,b.city,b.dist,b.addr,c.logistics,c.logistics, from order a left join customer b on a.customerid = b.customerid left join orderlogistics c on b.userid = c.userid

天蓬老师

将order(订单表)选择为你的主表,
1、里面有customerid可以去joincustomer(客户表)去查用户信息【username,nicename,mobile,pro,city,dist,addr】,
2、里面有ordercode订单号可以joinorderlogistics(物流信息表)去查物流信息

迷茫

报错了:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from order a left join customer b on a.customerid = b.customerid left join order' at line 1

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号