SELECT column1,column2,max(column3) ,max(column4)
from table_column
group by column1,column2
uj5u.com熱心網友回復:
我這只是舉個例子,實際可能有100列資料,然后分組后只想取某一列時間最新的一條完整資料。
uj5u.com熱心網友回復:
select 欄位, unix_timestamp(column4) as colu from table_column group by column1, column2 order by colu desc limit 1;
unix_timestamp 時間戳
uj5u.com熱心網友回復:
select * from (
select a.*,
if(@column1=column1 and @column2=column2,@rownum:=@rownum+1,@rownum:=1) AS Rn,
@column1:=column1 ncolumn1,
@column2:=column2 ncolumn2
from table_column a,(select @rownum:=0,@column1:=null,@column2:=null) b
) c
where rn=1
uj5u.com熱心網友回復:
應該是要分組排序取第一條
select t1.* from
(SELECT column1,column2,max(column3) column3,max(column4) column4 from table_column group by column1,column2) t1
where 1>(select count(*) from (SELECT column1,column2,max(column3) column3,max(column4) column4 from table_column group by column1,column2) t2 where t1.column1=t2.column1,t1.column2=t2.column2 and t1.column4<t2.column4)
uj5u.com熱心網友回復:
先分組,在主鍵倒排序,取第一條
select 欄位 from table_column group by column1, column2 order by 主鍵 desc limit 1;
select * from (select 欄位, unix_timestamp(column4) as colu from table_column order by colu desc) group by column1, column2;
uj5u.com熱心網友回復:
select * from (
select a.*,
if(@column1=column1 and @column2=column2,@rownum:=@rownum+1,@rownum:=1) AS Rn,
@column1:=column1 ncolumn1,
@column2:=column2 ncolumn2
from table_column a,(select @rownum:=0,@column1:=null,@column2:=null) b
) c
where rn=1
這用法好高級,mysql用的不多,我研究下,感謝
uj5u.com熱心網友回復:
應該是要分組排序取第一條
select t1.* from
(SELECT column1,column2,max(column3) column3,max(column4) column4 from table_column group by column1,column2) t1
where 1>(select count(*) from (SELECT column1,column2,max(column3) column3,max(column4) column4 from table_column group by column1,column2) t2 where t1.column1=t2.column1,t1.column2=t2.column2 and t1.column4<t2.column4)
這個原理不清楚,只是知道group by 獲取的分組值是第一條,那就把資料提前按照規定排好,然后group by。
uj5u.com熱心網友回復:
SELECT
*
FROM
table_name t1
JOIN (
SELECT
column1,
column2,
MIN(column4) column4
FROM
table_name
GROUP BY
column1,
column2
) t2 ON
t2.column1 = t1.column1
AND
t2.column2 = t1.column2
AND
t2.column4 = t1.column4;
uj5u.com熱心網友回復:
select * from table where id = (select id from table where col1 = col1 and col2 = col2 order by col3 limit 1)
手打的,別名什么都沒加,需要改一下
uj5u.com熱心網友回復:
select c1,c2,c3,c4
from table
group by c1,c2
having c4=max(c4)
為什么上面兄弟寫的那么麻煩?
uj5u.com熱心網友回復:
可以這樣select column1,column2,MAX(column3),MAX(column4) from a group by column1,column2
也可以使用rand去隨機選一個column3
uj5u.com熱心網友回復:
先分組,在主鍵倒排序,取第一條
select 欄位 from table_column group by column1, column2 order by 主鍵 desc limit 1;
這個是應該是新版mysql group by的雙刃劍,之前select的結果只能是在groupby中的列,現在select的列,可以不是groupby中的列,那么這個時候不在groupby中的列應該會有多條記錄,原來的mysql會報,子查詢中回傳的結果不是一條,新版的就直接默認給取第一條了,有時候利用這個小特性,還能實作oracle中的row_number() over(partition by)實作的功能
**桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......
我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......
關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......