如下:订单表关联了用户的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>

mysql关联模糊查询他表字段的更多相关文章

  1. mysql 实行模糊查询 一个输入值匹配多个字段和多个输入值匹配一个字段

    mysql 实行模糊查询  一个输入值匹配多个字段 MySQL单表多字段模糊查询可以通过下面这个SQL查询实现 为啥一定要150字以上  真的麻烦  还不让贴代码了 SELECT * FROM `ma ...

  2. mysql中模糊查询的四种用法介绍

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  3. Mybatis使用MySQL进行模糊查询时输入中文检索不到结果

    Mybatis使用MySQL进行模糊查询时输入中文检索时,需要在jdbcURL后增加参数   ?useUnicode=true&characterEncoding=UTF-8

  4. 下面介绍mysql中模糊查询的四种用法:

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

  5. MySQL数据库中查询数据库表、字段总数量,查询数据总量

    最近要查询一些数据库的基本情况,由于以前用oracle数据库比较多,现在换了MySQL数据库,就整理了一部分语句记录下来. 1.查询数据库表数量 #查询MySQL服务中数据库表数据量 SELECT C ...

  6. mysql 面试题 查询出表中某字段的重复值

    users 表中有 两个字段  id 和 name 表数据大概如下: id       name 1       AAA 2       BBB 3       CCC 4       AAA 请写查 ...

  7. mysql的模糊查询

    mysql模糊查询like/REGEXP(1)like / not like MySql的like语句中的通配符:百分号.下划线和escape %:表示任意个或多个字符.可匹配任意类型和长度的字符. ...

  8. mysql精准模糊查询使用CONCAT加占位符(下划线“_”)的使用,直接限定了长度和格式

    比如现在有张表t_user,如下:(表中只是引用某某某的话,并无恶意) id name 1 司马懿 2 司马老贼 3 司马老贼OR司马懿 4 司马大叔 1.模糊查询一般用的模糊查询都是like关键词, ...

  9. mysql update 子查询锁表问题

    mysql在Update带有子查询的时候,子查询的表会锁住,导致该表无法使用.比如 update A set comments = (select count(1) from B where id = ...

随机推荐

  1. [SDOI2013] 直径

    传送门:>HERE< 题意:给出一颗树,求出被所有的直径都经过的边的数量 解题思路: 先求出任意一条直径并记录节点. 然后依次枚举直径上的每一个节点,判断从当前节点延伸出去的非直径的一条路 ...

  2. hbase系列

    jvmhttps://www.cnblogs.com/jiyukai/p/6665199.html hbase https://blog.csdn.net/lizhitao/article/detai ...

  3. NEW —— Code

    http://ai.baidu.com/ 百度AI开放平台

  4. 【CF671D】Roads in Yusland(贪心,左偏树)

    [CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...

  5. webpack入门(五)webpack CLI

    webpack的CLI安装和命令 Installation $ npm install webpack -g The webpack command is now available globally ...

  6. ArcGIS for qml - 地址地标转换为经纬度(地理编码)

    实现输入地址地标转换为其经纬度 本文链接:地理编码 作者: 狐狸家的鱼 Github: 八至 一.地理编码 1.地理编码含义 地址编码(或地理编码)是使用地址中包含的信息来插入地图上的相应位置的过程. ...

  7. 洛谷P3354 河流

    有点权边权的树,选出k个关键点,根必须选.每个点的贡献为点权 * 到最近的关键祖先的距离.求最小总贡献. 解:树形DP是最毒瘤的算法...... 设fxij表示以x为根的子树中选了j个关键点,且x的最 ...

  8. 【洛谷P2860】冗余路径

    题目大意:给定一个 N 个点,M 条边组成的无向图,求至少在图中加入几条边才能使得整个图没有割边. 题解:求出该无向图的所有边双联通分量,每个边双联通分量可以理解成无向图的一个极大环,对该无向图进行缩 ...

  9. '新', '泽' - ImageMagick - UTF-8非最短形式及编码安全问题

    最近偶然发现,把软件放到 [新建文件夹]  中,ImageMagick 竟无法正常的加载图片了. 我去!什么情况? 抛出的错误是找不到相关的dll,软件中已对中文进行了utf-8编码,这几年来一直没发 ...

  10. latex 双引号 “

    别在latex敲,在记事本上敲完后,拷贝到latex中.