记录测试工作中,Mysql 数据库常用的一些 sql查询语句

记录测试工作中,常用的一些sql语句

Navicat连接测试Mysql数据库

1.查询项目名称为:HMFtest和业务类型为:传播-论坛的信息

多表查询(这里是三表)需要用到LEFT Join  ON

t1project  项目表
t1projectbusitype  项目类型表
t1probusitypedata  项目类型数据表

例子:

SELECT
    *
FROM
  t1project tp
LEFT JOIN t1projectbusitype tbt ON tbt.FProjectMainStruID = tp.FID
LEFT JOIN t1probusitypedata tbtd ON tbtd.FID = tbt.BUSITYPEID
WHERE
    tp.FNAME = "HMFtest" and tbtd.FNAME="传播-论坛"

2.查询(项目名称为:HMFtest,业务类型为“创意-视频”)中有效的订单明细:

多表查询:5张表

扫描二维码关注公众号,回复: 11080502 查看本文章

t1costsettle  订单成本结算
t1costsettledetail 订单成本结算明细表

t1project  项目表
t1projectbusitype  项目类型表
t1probusitypedata  项目类型数据表

需要的字段:

IDENTIFY=1(如IDENTIFY=1代表有效的成本明细)
FAUDITSTATUS 订单审核状态(1-3为有效)
FBILLAUDITSTATUS 订单单据审核状态(审核状态-1,4为有效)
sql语句,查询到5条记录
SELECT tp.F1NAME,tbtd.FNAME,tsd.* from t1c1ostsettledetail tsd
LEFT JOIN  t1costsettle ts on tsd.FCostSettleMainStruID=ts.FID
LEFT JOIN t1project tp ON tp.FID = ts.PROJECTID
LEFT JOIN t1projectbusitype tbt ON tbt.FID = ts.PROBUSITYPEID
LEFT JOIN t1probusitypedata tbtd ON tbtd.FID = tbt.BUSITYPEID
where tp.FNAME="HMFtest" and tbtd.FNAME="创意-视频" and tsd.FAUDITSTATUS in (1,3) and tsd.FBILLAUDITSTATUS not in (-1,4) and tsd.IDENTIFY=1

3.查询用户:胡的订单列表并按日期倒序排序

SELECT
    ti.title,
    ti.inquiry_date_start,
    ti.inquiry_date_end,
    ti.inquiry_type,
    ti.inquiry_status,
    tuser.FNAME,
  ti.create_date
FROM
    t_inquiry ti
LEFT JOIN tuser ON tuser.FID = ti.create_person_id
WHERE
    tuser.FNAME = "胡"
ORDER BY ti.create_date DESC

5.查询创建人为“王燕”和订单编号为19030011的订单详细信息

SELECT * from t1costsettledetail s1,tuser s2 where s1.MARKBILLPERSONID=s2.FID and s2.FNAME="王燕" and s1.FBILLSERIALNUM="19030011"

6.查询价格为第2高的订单信息

价格:price  表名:t1costsettledetail

6.1.最大价格的订单信息:select max(price) from t1costsettledetail

6.2.查询价格为第2高的订单信息

select max(price) as secondprice from t1costsettledetail where price <(SELECT max(price) from t1costsettledetail )

或者:

select Distinct price from t1costsettledetail  order by price DESC limit 1,1

limi 1,1 -->检索从第2行开始,累计1行(倒序第2就是第2高)

第n高的订单信息,例如第5

select Distinct price from t1costsettledetail  order by price DESC limit 4,1

7.对订单进行价格分组统计,并查询价格大于10000的订单分组统计数

SELECT price,count(*) from t1costsettledetail group by price HAVING price>10000

8.模糊查询用户/项目/订单

SELECT * from tuser where FNAME LIKE "%吴"

SELECT * from t1project where FNAME LIKE "%胡"

SELECT * from t1costsettledetail  where FNAME LIKE "%华为"

备注:LIMIT的使用例子

select * from t1costsettledetail  LIMIT 1;--检索前1行数据,显示1条数据

select * from t1costsettledetail  LIMIT 10;--检索前10行数据,显示1-10条数据
select * from t1costsettledetail  LIMIT 1,10;--检索从第2行开始,累加10条记录,共显示id为2....11
select * from t1costsettledetail  limit 5,10;--检索从第6行开始向前加10条数据,共显示id为6,7....15
select * from t1costsettledetail  limit 6,10;--检索从第7行开始向前加10条记录,显示id为7,8...16

select * from t1costsettledetail  LIMIT 1,1;--检索从第2行开始,累加1条记录(就是第2行记录)

如果价格不存在第2高的订单,那么查询应返回 null

select IFNULL((select Distinct price from t1costsettledetail  order by price DESC limit 1,1),null) 
as SecondHighestSalary

参考:https://www.cnblogs.com/yoyoblogs/p/5828651.html

发布了135 篇原创文章 · 获赞 6 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/fen_fen/article/details/105686582