[轉]Mysql將資料分組後取出時間最近的資料

來源:互聯網
上載者:User

標籤:大神   建立   color   back   rom   取出   esc   desc   資料   

如題,我在網上也找過相關解決方案,很多解答都是這麼一句SQL語句:
select Id,AccountId,Mark,max(CreateTime) as Latest from AccountMark as b group by AccountId 

使用Max函數。但是在我查出來的資料中似乎有些不對,,反白的那一條資料,Mark欄位和CreateTime欄位根本不對應啊!



這是怎麼回事?使用Max函數後在分組這樣靠譜嗎?

還有一條語句:select *,COUNT(AccountId) as Num from 
(select * from AccountMark order by CreateTime desc) `temp`  
group by AccountId order by CreateTime desc 
這樣查出來的資料是對的


但是,我需要建立視圖,Mysql中視圖裡不允許出現查詢子句。求大神些一條SQL語句,能實現既不出現子句,又能查出正確資料。謝謝!

 

解決方案:

select * from AccountMark as b where not exists(select 1 from AccountMark where AccountId= b.AccountId
and b.CreateTime<CreateTime )

 

原文地址:http://bbs.csdn.net/topics/390958705

[轉]Mysql將資料分組後取出時間最近的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.