思路:將兩列日期先格式化成以yyyy-mm-dd格式,再將兩列合併成一列,並且以逗號分隔開,再根據逗號將一列中的一行分割成多行,再分組去重
create
table
`t_product`
(`id`
varchar
(225
)not
null
,`start_time`
datetime
notnull
default
'0000-00-00 00:00:00'
,`end_time`
datetime
notnull
default
'0000-00-00 00:00:00'
,primary
key(
`id`))
engine
=innodb
default
charset
=utf8;
-- ----------------------------
-- records of t_product
-- ----------------------------
insert
into
`t_product`
values
('1'
,'2020-08-11 19:20:31'
,'2020-08-11 22:20:36');
insert
into
`t_product`
values
('2'
,'2020-08-12 19:09:09'
,'2020-08-12 22:20:36');
insert
into
`t_product`
values
('3'
,'2020-08-13 19:09:14'
,'2020-08-13 22:20:36');
insert
into
`t_product`
values
('4'
,'2020-08-11 19:06:59'
,'2020-08-14 22:20:36');
select
*from
(select substring_index(
substring_index(
all_date,
',',
b.help_topic_id +1)
,',',-
1)as all_date_group from
(select concat(date_format(p.start_time,
'%y-%m-%d'),
',', date_format(p.end_time,
'%y-%m-%d'))
as all_date from t_product p
) pp join mysql.help_topic b on b.help_topic_id <
( length(all_date)
- length(
replace
(all_date,
',','')
)+1)
) ss group
by ss.all_date_group
一列多行合併一列一行
方法1 定義臨時的varchar變數,通過游標一條條讀取,然後在迴圈中改變臨時變數的值最終輸出 關於游標的方法,這裡再敘述。sql2008中的方法 create table tb id int,value varchar 10 insert into tb values 1,aa insert in...
sql一列轉一行
自定義列名顯示 from criterion t feedingprogramstep 將表中的資料轉換成字串拼接進 var變數,每條資料之間以英文逗號分隔 order byinventoryproportion desc set var stuff var,1 1,去掉 var中的第乙個逗號 ex...
dataframe多列合併成一列
dataframe的幾列資料合併成為一列 dataframe的幾列資料合併成為一列 1.1 方法歸納 1.2 str.cat函式詳解 1.2.1 語法格式 1.2.2 引數說明 1.2.3 核心功能 1.2.4 常見範例 dataframe的幾列資料合併成為一列 1.1 方法歸納 使用 直接將多列合...