create table mobilelog(
id int primary key auto_increment, /* 自增id */
mobile varchar(26), /* 手機號碼 */
log varchar(55), /* 日誌資訊 */
createtime datetime /* 建立時間 */
獲取每個手機號碼最新的日誌資訊
先獲取所有號碼最新的id,此處需要同過id區分每條日誌,因為createtime可能重複。
select max(id) from mobilelog group by mobile;
通過id獲取日誌資訊:
select * from mobilelog where id in (select max(id) from mobilelog group by mobile);
獲取部分號碼的最新日誌資訊:
select * from mobilelog where id in
(select max(id) from mobilelog where mobile in ('15209864032','17209864032')
group by mobile);
select * from (select mobile,log from mobilelog order by mobile,id desc
)t group by t.mobile;
MySQL 分組後取最新值
1.需求 一張表中有多組資料,要求取出每組的最新一條的某些字段值。例如,成績表中儲存了學校所有學生 一年級 二年級 三年級 所有學科 語文 數學 英語 的成績,要求取出某個學生的最近一次所有學科的成績。2.實現 select 學科,select 成績from 成績表where 學科 a.學科 and...
SQL分組最大值
employee表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 henry 80000 2 3 sam 60000 2 4 max 90000 1 department...
分組取最新記錄的SQL
經常遇到這樣的情況,要取得所有客戶的最新交易記錄,讀取 所有瀏覽者最後一次訪問時間。乙個客戶只讀取最新的一次記錄,相同,大部分的人首先想 到的就是排除所有記錄,相同的只取一條。用distint,但是distint只能取到乙個欄位的值。所以使用distint就不能讀取 出所有的情況。下面是一條正確的語...