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>
mysql关联模糊查询他表字段的更多相关文章
- mysql 实行模糊查询 一个输入值匹配多个字段和多个输入值匹配一个字段
mysql 实行模糊查询 一个输入值匹配多个字段 MySQL单表多字段模糊查询可以通过下面这个SQL查询实现 为啥一定要150字以上 真的麻烦 还不让贴代码了 SELECT * FROM `ma ...
- mysql中模糊查询的四种用法介绍
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
- Mybatis使用MySQL进行模糊查询时输入中文检索不到结果
Mybatis使用MySQL进行模糊查询时输入中文检索时,需要在jdbcURL后增加参数 ?useUnicode=true&characterEncoding=UTF-8
- 下面介绍mysql中模糊查询的四种用法:
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
- MySQL数据库中查询数据库表、字段总数量,查询数据总量
最近要查询一些数据库的基本情况,由于以前用oracle数据库比较多,现在换了MySQL数据库,就整理了一部分语句记录下来. 1.查询数据库表数量 #查询MySQL服务中数据库表数据量 SELECT C ...
- mysql 面试题 查询出表中某字段的重复值
users 表中有 两个字段 id 和 name 表数据大概如下: id name 1 AAA 2 BBB 3 CCC 4 AAA 请写查 ...
- mysql的模糊查询
mysql模糊查询like/REGEXP(1)like / not like MySql的like语句中的通配符:百分号.下划线和escape %:表示任意个或多个字符.可匹配任意类型和长度的字符. ...
- mysql精准模糊查询使用CONCAT加占位符(下划线“_”)的使用,直接限定了长度和格式
比如现在有张表t_user,如下:(表中只是引用某某某的话,并无恶意) id name 1 司马懿 2 司马老贼 3 司马老贼OR司马懿 4 司马大叔 1.模糊查询一般用的模糊查询都是like关键词, ...
- mysql update 子查询锁表问题
mysql在Update带有子查询的时候,子查询的表会锁住,导致该表无法使用.比如 update A set comments = (select count(1) from B where id = ...
随机推荐
- ionic更改端口号
—— 重新指定端口号为8888 serve [options] ............................... 启动本地服务器进行开发测试 dev/testing [--console ...
- Git回滚代码暴力法
Git回滚有多种方式,这里使用的是[强制提交到远程分支] 效果为:如回滚前的提交记录是 1.2.3.4,使用这种方法回滚到2,那么提交记录就变成了1.2. 操作方法: 需要在本地的Git仓库,右键选择 ...
- EMM386和UMBPCI 区别
EMM386和UMBPCI 区别 1,SupportCD-ROM[HIMEM+EMM386NOEMS].支持光驱(EMM386模式)2,SupportCD-ROM[HIMEM+UMBPCI].支持光驱 ...
- 爬虫_猫眼电影top100(正则表达式)
代码查看码云
- Hdoj 1374.Knight Moves 题解
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- Hdoj 2018.母牛的故事 题解
Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Input 输入数据由多个测 ...
- django 模板语言
母版与继承: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 牛客练习赛31 D神器大师泰兹瑞与威穆
双链表搞完了 #include<bits/stdc++.h> using namespace std; #define maxn 1000005 int tot,bac[maxn],fa[ ...
- ACM-ICPC 2015 BeiJing
比赛连接:ACM-ICPC 2015 BeiJing 本次比赛只写了 A G 然后 I题随后补 A 有一个正方形土地,上面有若干块绿洲.让你以x0为界限划一条竖线,要求左边绿洲面积>=右 ...
- [nowcoderACM_223C][区区区间间间]
题目链接 思路 考虑用单调栈,栈顶为最大元素.当得到一个新值得时候,将这个值宇栈顶比较.因为栈顶是前面的最大元素.所以只要当前元素比栈顶大,那么肯定比前面的都大.只要将这个元素乘上前面的个数就行了. ...