如下:订单表关联了用户的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. GIL、进/线程池、同/异步、阻/非阻塞

    1 GIL:全局解释器锁 GIL本质就是一把互斥锁,是夹在解释器身上的, 同一个进程内的所有线程都需要先抢到GIL锁,才能执行解释器代码 2.GIL的优缺点: 优点: 保证Cpython解释器内存管理 ...

  2. 记一次Java调优案例分析

    上周,一同学给我发来,他们那里的案例 一看就是新生代产生过多对象,肯定是批量或者循环操作导致的,导致新生代一直在进行回收导致. 如果是老生代出现这样的问题,大部分情况下是列表或者集合导致的. 因此我们 ...

  3. MT【259】2016天津压轴题之最佳逼近

    (2016天津压轴题)设函数$f(x)=(x-1)^3-ax-b,x\in R$, 其中$a,b\in R$(1)求$f(x)$的单调区间.(2)若$f(x)$存在极值点$x_0$,且$f(x_1)= ...

  4. Linux 日志分析脚本

    #### 以下代码,若出现无法使用,请根据底下图片,更改参数.根据 apache 日志格式修改 查看 apache 进程ps aux | grep httpd | grep -v grep | wc ...

  5. Docker使用阿里云docker镜像加速

    首先进入阿里云docker库首页 https://dev.aliyun.com/ 点击 管理中心 点击 加速器 复制下面的加速地址 进入docker的 Settings 把basic 切换成 adva ...

  6. 区分IE8/IE7/IE6及其他浏览器

    在 CSS中常用特殊字符识别表: (1)*:  IE6+IE7都能识别*,而标准浏览器FF+IE8是不能识别*的; (2)!important: 除IE6不能识别 !important外,  FF+I ...

  7. iptables(2)

    MASQUERADE同样是做源地址转换,只不过防火墙会根据该策略自动查找可用的公网IP地址,适应变化的情况•若接口使用ppp+,表示匹配ppp0.ppp1……中任意可用的拨号连接•若需要演示操作,可以 ...

  8. Opennebula常用命令

    查看虚拟机状态信息: [oneadmin@localhost /]$ onevm list 查看虚拟机配置: [oneadmin@localhost /]$ onevm show 25 启动虚拟机: ...

  9. Docker安装及常用命令

    修改机器名: [root@docker /]# hostnamectl set-hostname Docker 安装EPEL源: [root@docker /]# yum -y install epe ...

  10. docker镜像操作

    1.获取镜像 docker pull NAME[:TAG] 如果不显式地指定TAG,则默认会选择latest标签,即下载仓库中最新版本的镜像.//获取最新镜像docker pull ubuntu // ...