mysql 中同時使用orderby和groupby的分析
最近做專案寫sql的時候同時使用到了order by 和 group by.然而查詢出來的結果不是自己想象中的那樣.
下面來分析一樣原因:
mysql 寫sql的順序:
select -> from-> where->group by->having->order by.
但mysql的解析器執行順序:
from-> where->group by->having->select->order by.
所以,從執行的流程來看,是先group by 然後在 order by.
order by拿到的結果裡已經是group by以後的結果.
因此,order by的欄位必須是group by 裡面已經存在的欄位.
相關推薦
mysql 中同時使用orderby和groupby的分析
最近做專案寫sql的時候同時使用到了order by 和 group by.然而查詢出來的結果不是自己想象中的那樣. 下面來分析一樣原因: mysql 寫sql的順序: selec
mysql中group by和order by同時使用無效的替代方案
前言 最近一年由於工作需要大部分使用的都是NoSql資料庫,對關係型資料庫感覺越來越陌生,一個由group by和order by 引發的血案由此而生。在此做個記錄,以備不時之需。 需求(得到所有barCode的最新的一條資料) 首先,看一下整體的表結構。
MySql中order by和union all同時使用
(SELECT * FROM `idents` where PhotoID = 'badc74d6-58bd-4e46-9a48-aefb3487a61d' AND Selected = 'Yes' ORDER BY IdentTime desc LIMIT 1) UNION ALL (SELECT * F
關於mysql中觸發器old和new
let 技術分享 技術 delet png images 9.png ima image 1.當使用insert語句的時候,如果原表中沒有數據的話,那麽對於插入數據後表來說新插入的那條數據就是new,如圖所示: 2.當使用delete語句的時候,刪除的那一條數據相對於刪除
MySQL中基於mysqldump和二進制日誌log-bin二進制日誌進行邏輯備份以及基於時間點的還原
總結 mysql-bin lin .sql bin -h eat log-bin 之前 本文出處:http://www.cnblogs.com/wy123/p/6956464.html 本文僅模擬使用mysqldump和log-bin二進制日誌進行簡單
MySQL中橫表和豎表相互轉換
order by png image mar 插入數據 group src then images 一 豎表轉橫表 1. 首先創建豎表 create table student ( id varchar(32) primary key, name varchar
mysql中engine=innodb和engine=myisam的區別(轉)
支持 innodb 增加 nbsp com 外鍵 iam ges blog 轉自http://blog.csdn.net/lingyi_xu/article/details/5393791 innodb引擎和mysiam引擎的區別 引擎 事務
006-MySQL中使用SHOW PROFILE命令分析性能
顯示 lec image off 性能 syn 功能 使用 lock 一、概述 1、版本支持 Show profiles是5.0.37之後添加的,要想使用此功能,要確保版本在5.0.37之後。 查看數據庫版本: Select version();
Mysql中函數和存儲過程的區別
enter sql 名稱 gpo urn 構造 結果 返回 class Mysql中函數和存儲過程的區別 存儲過程: 1、 可以寫sql語句 2、 inout,out構造返回值 3、 調用:call:存儲過程名稱 4、 可以返
mysql中的鍵和索引
mysql中的鍵和索引一、主鍵 索引 關系 1.主鍵:主鍵的唯一作用就是唯一標識表中的某一行數據。分為單一主鍵和聯合主鍵:單一主鍵:只用一列就能唯一標識一行。聯合主鍵:當使用一列已經不能唯一標示一行的時候,就要采用多列唯一標識一行,就是聯合主鍵。聯合主健多個字段不能同時相同 2.索引:索引的作用就是提高數據的
正確理解MySQL中的where和having的區別(轉載)
pri keyword 均值 VG pan group 解釋 having sele 下面以一個例子來具體的講解: 1. where和having都可以使用的場景 select goods_price,goods_name from sw_goods where g
Mysql中-Xtrabackup備份和恢復應用
images 選項 SQ oca memory 使用方法 doc 如果 def 關於Xtrabackup(又或innobackupex)的介紹,詳細參考官方文檔 Xtrabackup安裝指南 文件準備 [root@wuxiang11 ~]# cd percona-xtr
mysql中on關鍵字和where關鍵字
數據 col 區別 類型 常用 行數據 隱式 沒有 連接 在mysql的from子句中存在多表時可以選擇添加join關鍵字用來顯式的表明連接類型,如果不使用join關鍵字則為隱式連接(我的理解就是對表進行笛卡爾積),隱式連接沒有表明連接條件,使用where關鍵字對連接結
MySql : varchar 和varchar 的區別 mysql int(10)的含義 MySql中varchar(10)和varchar(100)的區別==>>以及char的利弊
mysql int(10)的含義 int(M) M表示最大顯示寬度。最大有效顯示寬度是255。顯示寬度與儲存大小或型別包含的值的範圍無關。 在 int(M) 中,M 的值跟 int(M) 所佔多少儲存空間並無任何關係。 int(1)
mysql中myisam引擎和innodb引擎的比較 以及 utf8_bin和utf8_general_ci的比較
一:Mysql中MyISAM引擎和InnoDB引擎的比較 結論 如果不清楚自己應該用什麼引擎,那麼請選擇InnoDB,Mysql5.5+的版本預設引擎都是InnoDB,早期的Mysql版本預設的引擎是MyISAM(ISAM) MyISAM 和 InnoDB的適用場景 MyISA
MySQl中的\g和\G
1. \g 在MySQL的sql語句後加上\g,效果等同於加上定界符,一般預設的定界符是分號; 2. \G 在MySQL的sql語句後加上\G,表示將查詢結果進行按列列印,可以使每個欄位列印到單獨的行。即將查到的結構旋轉90度變成縱向; (1)使用前,顯示效果很差 &nb
MySQL中int(M)和tinyint(M)數值型別中M值的意義
我們建立一個數據表test: mysql> CREATE TABLE test( -> id1 int(1), -> id2 tinyint(1) ->); 我們給id1定義為int,並設定字元長度為1,id2定
MySQL中int(10)和int(11)的區別+示例
我們都知道MySQL中的int(10)和int(11)在寬度上是不一樣的,但是其實區別真不大,因為都是佔用4個位元組,所以範圍都是一樣的,那麼主要的區別在哪兒呢? 今天忽然想到這個問題,因此做了一下測試: 1、建立一個測試用的表test,裡面只有兩個欄位,一個int10,一個int11 2、往表中插
mysql中find_in_set使用和in的區別
插入三條資料; INSERT INTO `test` VALUES (1, 'name', 'daodao,xiaohu,xiaoqin'); INSERT INTO `test` VALUES (2, 'name2', 'xiaohu,daodao,xiaoqin'); INSERT