在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。其原因是 distinct只能返回它的目标字段
,而无法返回其它字段,这个问题让我困扰了很久,用distinct不能解决的话,我只有用二重循环查询来解决,而 这样对于一个数据量非常大的站来说,无疑是会直接影响到效率的。所以我花了很多时间来研究这个问题
mysql的DISTINCT的关键字有很多你想不到的用处
1.在count 不重复的记录的时候能用到
比如SELECT COUNT( DISTINCT id ) FROM tablename;
就是计算talbebname表中id不同的记录有多少条
2,在需要返回记录不同的id的具体值的时候可以用
比如SELECT DISTINCT id FROM tablename;
返回talbebname表中不同的id的具体的值
3.上面的情况2对于需要返回mysql表中2列以上的结果时会有歧义
比如SELECT DISTINCT id, type FROM tablename;
实际上返回的是 id与type同时不相同的结果,也就是DISTINCT同时作用了两个字段,必须得id与tyoe都相同的才被排除了,与我们期望的结果不一样
例
代码如下 | 复制代码 |
CREATE TABLE `student` ( |
1.测试一
select * from student;
a 5
a 5
c 0
用distinct过滤掉两列都相同的记录
代码如下 | 复制代码 |
select distinct name,age from student; |
返回
a 5
c 0
2.测试二
将表student的数据改为如下:
代码如下 | 复制代码 |
select * from student; select distinct name,age from student; |
返回如下,说明distinct后面有多于一列的字段时,只有每列的值完全相同才过滤
c 2
c 5
3.测试三
代码如下 | 复制代码 |
select * from student; name age height |
c 2 123
c 2 456
b 20 222
代码如下 | 复制代码 |
group by按两列同时分组 |
group by按两列同时分组,同时在后面加上having的条件
代码如下 | 复制代码 |
select name,age,sum(height) as n from student group by name,age having n > 500; |
返回
c 2 579
4.测试四
关于group by后面limit的测试
代码如下 | 复制代码 |
select songname,sengerid,count(sengerid) as n from t_song group by songname,sengerid having n > 1 ORDER BY n DESC,songid ASC limit 10; |
未知 8738 40
共同渡过 1432 24
风继续吹 1432 23
倩女幽魂 1432 23
无心睡眠 1432 23
罗百吉超嗨派对连续组曲 780 19
拒绝再玩 1432 19
风再起时 1432 18
每天爱你多一些 1480 18
千言万语 1794 18
代码如下 | 复制代码 |
select songname,sengerid,count(sengerid) as n from t_song group by songname,sengerid having n > 1 ORDER BY n DESC,songid ASC limit 5; |
未知 8738 40
共同渡过 1432 24
风继续吹 1432 23
倩女幽魂 1432 23
无心睡眠 1432 23
经过以上两个测试可以看出,如果sql语句中含有limit,limit是对用group by进行分组,并进行相关计算以后的limit操作,而不是对limit后面的指定记录数进行分组,从n那一列的数据每一行的值都大于10就可以看出来。
5.测试五
用以下的两种形式的distinct均可以得到相同的记录数,写法不一样,结果是一样的。
代码如下 | 复制代码 |
select count(distinct(songid)) from feedback; select count(distinct songid) from feedback; |
6.测试六
field singername is string,max(singername),如果singername有些列为空,有些列不为空,则max(singername)取非空的值,如果一列为zxx,一列为lady,则取zxx,按字母顺利取的。
代码如下 | 复制代码 |
select feedback_id,songid,songname,max(singername),max(time) as new_time from feedback group by songid order by new_time desc; |
7.Sql语句中where,group by,order by及limit的顺序
where xxx,group by xxx,order by xxx,limit xxx
8.关于group by与count的问题
如果sql语句中含有group by,则最好不要将count sql转换为select count(*) from xxx,否则select与from之间的字段很有可能是后面要使用的,例如
代码如下 | 复制代码 |
select feedback_id,songid,songname,max(singername),max(time) as new_time from feedback group by songid order by new_time desc; MySQL Query Error: SELECT COUNT(*) FROM feedback GROUP BY songid ORDER BY new_time DESC Error Info:Unknown column 'new_time' in 'order clause' |

todropaviewinmysql、 "dropviewifexistsview_name;" andtomodifyaviewを使用して、 "createorreplaceviewview_nameasselect ..."を使用します

mysqlviewscanefectiveativeativeizedesignpatternslikeadapter、decorator、factory、andobserver.1)adapterpatternadaptsdatafromdifferenttablesintoaunifiedview.2)decoratorpatternenhancesdatawithedfieldsfieldsiffieldsiffieldsiffiedを

viewsinmysqlarebenefentialforsimprifiningcomplexqueries、拡張セキュリティ、ダタコンシーニング、および最適化されたパフォーマンスを保証する1)itsmplifyififycomplexqueriesbyencapsulsingthemintoreusableviews.2)viewsencurationecuritybycontrollingcescesces.3)

to CreateAsimpleviewinmysql、usethecreateviewstatement.1)DefinetheTheTheThecreateview_nameas.2)SpecifyTheSelectStatementtatementtatementtatementtatementtatementtatementedeSireddata.3)

tocleateusersinmysql、usethecreateuserstatement.1)foralocaluser:createUser'localuser '@' localhost'identifidedifiedifiedified 'securepassword';

mysqlviewshavelimitations:1)supportallsqloperations、制限、dataManipulationswithjoinsorubqueries.2)それらは、特にパフォーマンス、特にパルフェクソルラージャターセット

reperusermanmanagementInmysqliscialforenhancingsecurationsinginuring databaseaperation.1)usecreateusertoaddusers、指定connectionsourcewith@'localhost'or@'% '。

mysqldoes notimposeahardlimitontriggers、しかしpracticalfactorsdeTerminetheireffectiveuse:1)serverconufigurationStriggermanagement; 2)complentiggersincreaseSystemload;


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。
