如下:订单表关联了用户的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. Scrapy爬取伯乐在线文章

    首先搭建虚拟环境,创建工程 scrapy startproject ArticleSpider cd ArticleSpider scrapy genspider jobbole blog.jobbo ...

  2. 【AGC005F】Many Easy Problems FFT 容斥原理

    题目大意 给你一棵树,有\(n\)个点.还给你了一个整数\(k\). 设\(S\)为树上某些点的集合,定义\(f(S)\)为最小的包含\(S\)的联通子图的大小. \(n\)个点选\(k\)个点一共有 ...

  3. CCPC-Wannafly Winter Camp Day1 (Div2, onsite) A B C E F I J

    A 机器人 链接:https://www.cometoj.com/contest/7/problem/A?problem_id=92 思路: 分两大类讨论: 1. B区没有点: (1)点都在起点左边 ...

  4. 阿里云.log

    申请证书审核失败的原因及处理方法;( 新添加站点 免费版 SSL 网页内不能有 HTTPS的连接:更多点击连接)

  5. MT【268】投篮第一次很重要

    已知 $r_1=0,r_{100}=0.85,(r_k$ 表示投 k 次投中的概率.)求证:(1)是否存在$n_0$使得$r_{n_0}=0.5$ (2)是否存在$n_1$使得$r_{n_1}=0.8 ...

  6. BZOJ 4242: 水壶(Kruskal重构树 + Bfs)

    题意 一块 \(h ∗ w\) 的区域,存在障碍.空地.\(n\) 个建筑,从一个建筑到另一个建筑的花费为:路径上最长的连续空地的长度. \(q\) 次询问:从建筑 \(s_i\) 到 \(t_i\) ...

  7. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  8. [算法进阶0x10]基本数据结构C作业总结

    t1-Supermarket 超市利润 题目大意 给定n个商品,每个商品有利润pi和过期时间di.每天只能卖一个商品,过期商品不能卖.求如何安排每天卖的商品可以使收益最大. 分析 一开始打了一个复杂度 ...

  9. pip 安装第三方包提示Unknown or unsupported command 'install'

    Unknown or unsupported command 'install' Unknown or unsupported command 'show' Unknown or unsupporte ...

  10. 工作机制.py

    rpc_server.py# !/usr/bin/env python3.5# -*- coding:utf-8 -*-# __author__ == 'LuoTianShuai'"&quo ...