distinct left mysql,mysql 语句group by,distinct, FIND_IN_SET, left join

标签:

group by 和 distinct的区别

FIND_IN_SET语法

left join

group by 和 distinct的区别:

58803bafbfcde454be66eeff495e1d84.png

共同点: 去重

group by

select cProcedureID, updateTime, Num  from i_plc_procedure_record where updateTime > "2018-09-04 00:00:00" group by cProcedureID

341864c74dfb018237aae407d12f9160.png

distinct:

distinct 字段需放在字段最前边, 要不然会报错, 去重

错误方法:

select  cProcedureID,distinct updateTime, Num  from i_plc_procedure_record where updateTime > "2018-09-04 00:00:00" group by cProcedureID

select  cProcedureID,distinct updateTime, Num  from i_plc_procedure_record where updateTime > "2018-09-04 00:00:00" group by cProcedureID

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'distinct updateTime, Num  from i_plc_procedure_record where updateTime > "2018-0' at line 3

正确使用方法:

select distinct cProcedureID, updateTime, Num  from i_plc_procedure_record where updateTime > "2018-09-04 00:00:00" group by cProcedureID

不同点: group by 的功能比distinct更加强大

group by 聚合函数,

GROUP BY 语法

SELECT column_name, function(column_name)

FROM table_name

WHERE column_name operator value

GROUP BY column_name;

语句使用:

select cProcedureID, max(updateTime) as updateTime, SUM(Num*unitPrice) as Money, Num  from i_plc_procedure_record where updateTime > "2018-09-04 00:00:00" group by cProcedureID

2d87d1bf5696e3b4b7c06ec408cc477a.png

FIND_IN_SET语法:

mysql 语法:

FIND_IN_SET(str,strlist)

str 要查询的字符串

strlist 字段名 参数以”,”分隔 如 (1,2,6,8)

查询字段(strlist)中包含(str)的结果,返回结果为null或记录

FIND_IN_SET和IN 之间的关联

相同点

SELECT accessWebID FROM i_plc_authority where FIND_IN_SET(id,(select authorityID from i_plc_users where id =UserID))

SELECT accessWebID FROM i_plc_authority where id in (select authorityID from i_plc_users where id =UserID)

区别点

select * from i_plc_product where FIND_IN_SET("1", cInvName) and operationFlag=9  正常运行

select * from i_plc_product where  "1" in cInvName and operationFlag=9  报错

[SQL] -- select * from i_plc_sfc_operation where operationFlag=9 order by opCode + 0

select * from i_plc_product where  "1" in cInvName and operationFlag=9

[Err] 1064- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cInvName and operationFlag=9' at line 2

left join:

left join 和 where的区别

select w.OpSeq,w.WcCode,w.id,c.WcName from technic1workcenter as w left join (select WcName,WcCode from i_plc_workcenter where operationFlag=9) c on c.WcCode=w.WcCode where w.operationFlag=9

a41c8a986b36bef5692249f5adb25081.png

select w.OpSeq,w.WcCode,w.id,c.WcName from technic1workcenter as w left join i_plc_workcenter c on c.WcCode=w.WcCode where w.operationFlag=9 and c.operationFlag=9

1570366144e8bacc301fe52e17d20210.png

(一)查询的数据为什么比(二)多?  是因为technic1workcenter的数据没有与i_plc_workcenter关联, 但是在(二)中存在 where c.operationFlag=9, 导致直接过滤掉了

select w.OpSeq,w.WcCode,w.id,c.WcName from technic1workcenter w, i_plc_workcenter c where c.WcCode=w.WcCode and w.operationFlag=9 and c.operationFlag=9

208f6f428c12087fce581d6a6c9b8b31.png

where类似于inner join。

标签:

来源: https://blog.csdn.net/u012447842/article/details/82628914

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值