MYSQL<小筆記1– 3>

paul chou
4 min readAug 28, 2020

這一章節,我想開始介紹一些比較有趣的東西,也就是MYSQL的功能,如果上網查詢 ” MYSQL function ”會查到很多種指令,像是變大寫、組合、又或者是顯示有多少字,這種function。就像以下這樣。網址我會貼在最下面,那這一章節就是在講我比較常用的功能,因為不一定全部的指令都要很熟悉,有需要再去查就好。

MYSQL的功能

這邊我會用一個書店的範例來展示這些我想分享的function,我創了一個database 裡面是關於一間書店的資料,裡面有書的id、書名、作者的姓、作者的名、有多少庫存、幾年出版的書、倉庫有多少書、跟書的頁數。至於怎麼創造這些Table我在小筆記1–1跟小筆記1–2有介紹,裡面的data我有都創好了。

concat:

這個功能是 Return concatenated string 中文的意思就是將2個資料合在一起。

舉例:我要把作者的姓跟名一起顯示的話

select concat(author_fname, author_lname) from books;

substring:

這個功能是 Return the substring as specified 也就是字串切割的意思。

舉例:我只要顯示書名的前10個字就好

select substr(title, 1,10) from books;

當然這邊的1到10是第1個字到第10個字的意思。

replace:

Replace occurrences of a specified string 顧名思義就是取代的意思

舉例:我要把書名的字母e都取代成$符號

select replace(title,’e’,’$’) from books;

reverse:

Reverse the characters in a string 這字面上的意思就是相反 好比說abc相反就是cba

舉例:

我要把書名全部都變成相反的字:

select reverse(title) from books;

upper、lower:

大寫或小寫的意思

舉例:

把書名都變成大寫:

select upper(title) from books;

這些是關於string的功能,好那我們現在來組合一下這些功能吧!

比方說我想要把書名的字都變大寫並且要有名跟姓!給你們2分鐘作答!

想好了嗎?我要公布答案囉!

解答:

select upper(concat(author_fname,author_lname)) from books;

再來一題!我要一個每本書名跟他的出版時候 而且要顯示成書名 released in 年像是(xx released in xxxx)

解答:

select concat(title,’ released in ’,released_year) from books;

function 的網站:https://dev.mysql.com/doc/refman/8.0/en/string-functions.html

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response