mysql去重彙總 Mysql常用SQL彙總

2021-10-18 22:05:13 字數 1789 閱讀 9946

今天的部落格主題

資料庫篇 --》mysql --》mysql常用sql彙總

目錄字段合併去重

查詢結果追加自定義字串

查詢當天的資料

字段合併去重

今天工作當中被同事問道資料庫需要兩個字段合併去重。簡單粗暴點,直接上code 。

表資料

需求

來吧三部曲

第一步:合併aa bb 字段

注意:這一步合併的時候根據欄位的分割符號需要根據原始資料分割符號來擇選,如果不一樣,可以先臨時統一符號。

第二步:拆分cc欄位

第三步:去重合併cc欄位

select id, group_concat(distinct aa.cc) from (

select

a.id,

substring_index(

substring_index(

a.cc,

b.help_topic_id + 1

), ',', - 1

) cc

from

select id, concat(aa,',',bb) cc from temp_0226

) ajoin mysql.help_topic b on b.help_topic_id <

length(a.cc) - length(replace(a.cc, ',', '')) + 1

) aa group by id

ok。大功告成。

如果想要其他欄位在第二步a.id後面追加即可!

此法可能會比較笨,自己琢磨寫的。如有sql大神望不吝賜教。。。

查詢結果追加自定義字串

select concat(name, '最棒的') from user ;

查詢當天的資料

select * from sys_user where to_days(last_login_name) = to_days(now());

給大家分享個sql技巧

不管多長的sql,都是從最中心開始往外寫的,根據需求來寫,第一步想要什麼第二步想要什麼這樣一直往外寫。

一樣當你去檢視別的寫的sql,一看哇這麼長,當時就想放棄了,越這樣越學不到知識。

把每次搜尋引擎到的好文章好code或者自己寫的都記錄下來,方便下次使用!!!

mysql去重欄位 mysql多字段去重,並計數

問 題 mysql版本5.5.42 有兩個表,表結構與資料如下 1 goods表 create table goods id int 10 unsigned not null,product varchar 180 collate utf8mb4 unicode ci not null,size v...

mysql 去重方式

distinct group by 不能與order by一起使用,細節看執行計畫 exists 某些地方推薦,資料越多越好用 對下面這條語句去重,查詢資料多時優化,資料多不推薦 select 1 from a r a left join t a m b on a.id b.pid left joi...

mysql 查詢 去重

insert into material id file url select id,ekm.file url from extmaterial as ekm left join select file url from material where id id as pm on pm.file u...