zoukankan      html  css  js  c++  java
  • mysql关联模糊查询他表字段

    如下:订单表关联了用户的id(多个),要根据用户名模糊查询订单信息,但是订单表只有id。创建视图用不着,咱也没权限。于是如下

    SELECT * FROM (
    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',cs.cName AS 'cName',se.sName AS 'serviceType',coop.ctName AS 'cooperationName'
    FROM `customerorder` AS cu
    LEFT JOIN `userdetail` AS us1 ON us1.id=cu.uid
    LEFT JOIN `userdetail` AS us2 ON us2.id=cu.updateUserid
    LEFT JOIN `customer` AS cs ON cs.id=cu.cid
    LEFT JOIN `servicetype` AS se ON se.id=cu.orderType
    LEFT JOIN `cooperationtype` AS coop ON coop.id=cu.cooperationtypeid)
    AS `customerorder`
    WHERE cCreateTime >='2018-1-1 01:10:11' AND cCreateTime<='2018-11-30 10:00:15'
    AND serviceuName LIKE CONCAT('%','业务','%')
    ORDER BY cCreateTime ASC
    LIMIT 0,10

    总结:

    1.为什么用left join 而不是join 或者inner join,你试一下就知道了。后两者在数据匹配不到的情况下整条记录都没有。

    2.时间类型用的是timestamp,因为方便省空间。但是时间段查询用bewteen就不行,所以只能用>=,<=

    顺便记录mybatis语句:

    SELECT * FROM (
    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',cs.cName AS 'cName',se.sName AS 'serviceType',coop.ctName AS 'cooperationName'
    FROM `customerorder` AS cu
    LEFT JOIN `userdetail` AS us1 ON us1.id=cu.uid
    LEFT JOIN `userdetail` AS us2 ON us2.id=cu.updateUserid
    LEFT JOIN `customer` AS cs ON cs.id=cu.cid
    LEFT JOIN `servicetype` AS se ON se.id=cu.orderType
    LEFT JOIN `cooperationtype` AS coop ON coop.id=cu.cooperationtypeid)
    AS `customerorder`
    <where>
    <if test="cid !=null and cid !=''">AND cid=#{cid}</if>
    <if test="uName !=null and uName !=''">AND serviceuName LIKE CONCAT('%',#{uName},'%')</if>
    <if test="startTime !=null">AND cCreateTime >=#{startTime}</if>
    <if test="endTime !=null">AND #{endTime}>=cCreateTime</if>
    <if test="orderBy==5">AND alreadyPaid>=totalPrice</if>
    <if test="orderBy==6">AND totalPrice>alreadyPaid</if>
    </where>
    <if test="orderBy==1">ORDER BY totalPrice ASC</if>
    <if test="orderBy==2">ORDER BY totalPrice DESC</if>
    <if test="orderBy==3">ORDER BY cCreateTime ASC</if>
    <if test="orderBy==4">ORDER BY cCreateTime DESC</if>
    <if test="pagesize>0">limit #{index},#{pagesize}</if>
  • 相关阅读:
    转载:[Oracle]杀死正在执行的sql语句
    转载:记录一次MySQL两千万数据的大表优化解决过程
    转载:logback日志详解
    转载:MySQL千万级大表优化攻略
    使用dbUnit的 IDataSet 因乱序造成assert失败而采取的措施
    解锁用户scott并授权
    两表连接各种Join图示,SQL及查询结果
    一句Delete..In.. 删除语句的优化
    大数据技术之_08_Hive学习_02_DDL数据定义(创建/查询/修改/删除数据库+创建表+分区表+修改表+删除表)+DML数据操作(数据导入+数据导出+清除表中数据)
    大数据技术之_08_Hive学习_01_Hive入门+Hive安装、配置和使用+Hive数据类型
  • 原文地址:https://www.cnblogs.com/zeussbook/p/9999736.html
Copyright © 2011-2022 走看看