mysql查询语句我们都经常在用,今天维护数据库出现以下需求,mysql查询语句查出user表中不重复的记录,使用distinct但他只能对一个字段有效。下面学步园小编来讲解下mysql查询语句中distinct有哪些问题?mysql查询大小写有哪些问题?
mysql查询语句中distinct有哪些问题
原因就是distinct它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。
也就是distinct只能返回它的目标字段,而无法返回其它字段
例如:
SELECTDISTINCTmac,ipfromip+------+------+|mac|ip|+------+------+|abc|678||abc|123||def|456||abc|12|+------+------+
他还是不会有变换!因为上面的语句产生的作用就是作用了两个字段,也就是必须得mac与ip都相同的才会被排除
最后没有办法,使用groupby看看!!!!
查看mysql手册!connt(distinctname)可以配合groupby实现。
一个count函数实现我要的功能。
select*,count(distinctmac)fromipgroupbymac;+------+------+---------------------+|mac|ip|count(distinctmac)|+------+------+---------------------+|abc|678|1||def|456|1|+------+------+---------------------+
基本实现我的想法!
那如何实现一个表有两个字段mac和ip,如何找出所有的mac相同而ip不同的记录?
mysql>select*fromip;+-----+-----+|mac|ip|+-----+-----+|abc|123||def|456||ghi|245||abc|678||def|864||abc|123||ghi|245|+-----+-----+7rowsinset(0.00sec)mysql>SELECTDISTINCTa.mac,a.ip->FROMipa,ipb->WHEREa.mac=b.macANDa.ip<>b.ipORDERBYa.mac;+-----+-----+|mac|ip|+-----+-----+|abc|678||abc|123||def|864||def|456|+-----+-----+4rowsinset(0.00sec)
mysql查询大小写有哪些问题
mysql查询默认是不区分大小写的如:
selectfromtable_namewherealike'a'selectfromtable_namewherealike'a'selectfromtable_namewherealike'a'selectfromtable_namewherealike'a'
效果是一样的。
要让mysql查询区分大小写,可以:
selectfromtable_namewherebinaryalike'a'selectfromtable_namewherebinaryalike'a'selectfromtable_namewherebinaryalike'a'selectfromtable_namewherebinaryalike'a'
也可以在建表时,加以标识
createtabletable_name(avarchar(20)binary)
以上就是关于“mysql查询语句中distinct有哪些问题?mysql查询大小写有哪些问题”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!