mysql 5.7 及以下版本 分組(GROUP BY)和排序(ORDER BY)共同使用
《1》select * FROM gs_day_trade_list a WHERE
deal_time IN ( SELECT MAX( deal_time ) FROM gs_day_trade_list where data_date='2025-01-03' and trade_date='2025-01-03' GROUP BY data_period )
ORDER BY data_period asc,deal_time desc
1.思路先分組后面排序,以上 sql 實現(xiàn) 按照 時段分組 后,取 成交時間最新的一條數(shù)據(jù), 如果 時間最新早的一條, max 改成 min 就可以了
--以上有bug 時間有相同的就出來多余數(shù)據(jù)
《2》select * FROM gs_day_trade_list a WHERE
deal_time = ( SELECT MAX( b.deal_time ) FROM gs_day_trade_list b where a.data_date=b.data_date and a.trade_date=b.trade_date GROUP BY b.data_period )
where a.data_date='2025-01-03' and b.trade_date='2025-01-03'
ORDER BY a.data_period asc,a.deal_time desc

《3》
select GROUP_CONCAT(menu_id) from rh_menu; --所有行的字段用逗號拼接成一列
select CONCAT(menu_id,parent_id) from rh_menu --- 兩個字段拼接
select CONCAT_WS(',',menu_id,parent_id) from rh_menu --- 兩個字段用逗號拼接
select * from rh_menu a where FIND_IN_SET(a.parent_id,'0')=1 and a.sys_code='11' --- 查詢 parent_id等于0的數(shù)據(jù),F(xiàn)IND_IN_SET 可以放 函數(shù),字段 ,和 in 和like 類似
select * from rh_menu a where FIND_IN_SET(a.parent_id,get_child_list('0'))=1 and a.sys_code='11' --- 查詢 parent_id等于0的數(shù)據(jù),F(xiàn)IND_IN_SET 可以放 函數(shù),字段 ,和 in 和like 類似
《4》mysql 自定義 函數(shù)
delimiter $$
drop function if exists get_child_list$$
create function get_child_list(in_id varchar(10)) returns varchar(1000)
begin
declare ids varchar(1000) default '';
declare tempids varchar(1000);
set tempids = in_id;
while tempids is not null do
set ids = CONCAT_WS(',',ids,tempids);
select GROUP_CONCAT(id) into tempids from dept where FIND_IN_SET(pid,tempids)>0;
end while;
return ids;
end
$$
delimiter ;
參考
http://www.rzrgm.cn/guohu/p/14990788.html


浙公網(wǎng)安備 33010602011771號