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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM