mysql⼀张表多个字段关联另⼀张表查询如下:⼀张订单表多个字段关联⽤户表:
1.链表查询
SELECT cu.id AS 'id',
cu.version AS 'version',
cu.cid AS 'cid',
cu.uid AS 'uid',
cu.shopName AS 'shopName',
cu.address AS 'address',
alignment怎么用cu.state AS 'state',
cu.cCreateTime AS 'cCreateTime',
cu.decorate AS 'decorate',snmp配置
cu.area AS 'area',
mysql语句多表查询cu.machinePrice AS 'machinePrice',
cu.caid AS 'caid',小时代在线观看免费
cu.machineDiscount AS 'machineDiscount',
cu.alreadyPaid AS 'alreadyPaid',
cu.updateTime AS 'updateTime',
霸王别姬instrumental
cu.updateUserid AS 'updateUserid',
cu.overseerId AS 'overseerId',
cu.decorationQuotation AS 'decorationQuotation',
cu.machineDeposit AS 'machineDeposit',
us1.uName AS 'serviceuName',
us2.uName AS 'updateName'
FROM customerorder AS cu
LEFT JOIN userdetail AS us1
ON us1.id = cu.uid
LEFT JOIN userdetail AS us2
ON us2.id = cu.updateUserid;
2.⼦查询
SELECT cu.id AS 'id',
cu.version AS 'version',
cu.cid AS 'cid',
cu.uid AS 'uid',
cu.shopName AS 'shopName',
cu.address AS 'address',
cu.state AS 'state',
cu.cCreateTime AS 'cCreateTime',
cu.decorate AS 'decorate',
cu.area AS 'area',
cu.machinePrice AS 'machinePrice',
cu.caid AS 'caid',
cu.machineDiscount AS 'machineDiscount',
cu.alreadyPaid AS 'alreadyPaid',
cu.updateTime AS 'updateTime',
cu.updateUserid AS 'updateUserid',
cu.overseerId AS 'overseerId',
cu.decorationQuotation AS 'decorationQuotation',
cu.machineDeposit AS 'machineDeposit',
(SELECT uName FROM userdetail WHERE id = cu.uid) AS 'serviceuName',
(SELECT uName FROM userdetail WHERE id = cu.updateUserid) AS 'updateName'
FROM customerorder AS cu;
总结:
1,表关联的效率要⾼于⼦查询,因为⼦查询⾛的是笛卡尔积
2,表关联可能有多条记录,⼦查询只有⼀条记录,如果需要唯⼀的列,最好⾛⼦查询

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。