zoukankan      html  css  js  c++  java
  • 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',
    cu.totalPrice AS 'totalPrice',cu.orderType AS 'orderType',
    cu.state AS 'state',cu.cCreateTime AS 'cCreateTime',cu.decorate AS 'decorate',cu.area AS 'area',cu.roomArea AS 'roomArea',
    cu.machinePrice AS 'machinePrice',cu.caid AS 'caid',
    cu.cooperationtypeid AS 'cooperationtypeid',cu.cooperationRebate AS 'cooperationRebate',cu.cooperationPrcie AS 'cooperationPrcie',
    cu.machineDiscount AS 'machineDiscount',cu.alreadyPaid AS 'alreadyPaid',cu.updateTime AS 'updateTime',cu.cooperationdeposit AS 'cooperationdeposit',
    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.totalPrice AS 'totalPrice',cu.orderType AS 'orderType',
    cu.state AS 'state',cu.cCreateTime AS 'cCreateTime',cu.decorate AS 'decorate',cu.area AS 'area',cu.roomArea AS 'roomArea',
    cu.machinePrice AS 'machinePrice',cu.caid AS 'caid',
    cu.cooperationtypeid AS 'cooperationtypeid',cu.cooperationRebate AS 'cooperationRebate',cu.cooperationPrcie AS 'cooperationPrcie',
    cu.machineDiscount AS 'machineDiscount',cu.alreadyPaid AS 'alreadyPaid',cu.updateTime AS 'updateTime',cu.cooperationdeposit AS 'cooperationdeposit',
    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,表关联可能有多条记录,子查询只有一条记录,如果需要唯一的列,最好走子查询

  • 相关阅读:
    MySQL常用函数
    SQL之join
    java并发编程之三--CyclicBarrier的使用
    java并发编程之二--CountDownLatch的使用
    java并发编程之一--Semaphore的使用
    微信小程序 bindcontroltap 绑定 没生效
    报错:Syntax error on tokens, delete these tokens
    java创建类的5种方式
    数据类型转换
    JS数据结构算法---数组的算法
  • 原文地址:https://www.cnblogs.com/zeussbook/p/9999052.html
Copyright © 2011-2022 走看看