案例:使用各種函數操作數據,掌握各種函數的作用和使用方法。
(1)使用數學函數rand()產生3個10以內的隨機整數。
(2)使用sin(),con(),tan(),cot()函數計算三角函數值,並將計算結果轉換成整數值。
(3)建立表格,並使用字串和日期函數對欄位值進行操作。
(4)使用case進行條件判斷,如果m_birth
小於2000年,就顯示"old";如果m_birth大於2000年,則顯示"young"。
(免費學習推薦:mysql影片教學)
mysql> select round( rand() * 10 ),round( rand() * 10 ), round( rand() * 10 );+----------------------+----------------------+----------------------+| round( rand() * 10 ) | round( rand() * 10 ) | round( rand() * 10 ) |+----------------------+----------------------+----------------------+| 9 | 8 | 1 |+----------------------+----------------------+----------------------+1 row in set (0.00 sec)
mysql> select pi(), -> sin(pi()/2), -> cos(pi()), -> round(tan(pi()/4)), -> floor(cot(pi()/4));+----------+-------------+-----------+--------------------+--------------------+| pi() | sin(pi()/2) | cos(pi()) | round(tan(pi()/4)) | floor(cot(pi()/4)) |+----------+-------------+-----------+--------------------+--------------------+| 3.141593 | 1 | -1 | 1 | 1 |+----------+-------------+-----------+--------------------+--------------------+1 row in set (0.00 sec)
①建立表格member,其中包含5個字段,分別為auto_increment約束的m_id字段、varchar類型的m_FN字段、varchar類型的m_LN字段、datetime類型m_birth字段和varchar類型的m_info字段。
mysql> create table member -> ( -> m_id int auto_increment primary key, -> m_FN varchar(100), -> m_LN varchar(100), -> m_birth datetime, -> m_info varchar(255) null -> );Query OK, 0 rows affected (0.21 sec)
②插入一筆記錄,m_id值為預設,m_FN值為"Halen",m_LN值為"Park",m_birth值為1970-06-29,m-info值為"GoodMan"。
mysql> insert into member values -> ( null,'Halen','Park','1970-06-29','GoodMan');Query OK, 1 row affected (0.01 sec)mysql> select * from member;+------+-------+------+---------------------+---------+| m_id | m_FN | m_LN | m_birth | m_info |+------+-------+------+---------------------+---------+| 1 | Halen | Park | 1970-06-29 00:00:00 | GoodMan |+------+-------+------+---------------------+---------+1 row in set (0.00 sec)
③傳回m_FN的長度,傳回第一筆記錄中人的全名,將m_info欄位值轉換成小寫字母。將m_info的值反向輸出。
mysql> select length(m_FN),concat(m_FN,m_LN), -> lower(m_info),reverse(m_info) from member;+--------------+-------------------+---------------+-----------------+| length(m_FN) | concat(m_FN,m_LN) | lower(m_info) | reverse(m_info) |+--------------+-------------------+---------------+-----------------+| 5 | HalenPark | goodman | naMdooG |+--------------+-------------------+---------------+-----------------+1 row in set (0.00 sec)
④計算第1筆記錄中人的年齡,並計算m_birth字段中的值在那一年中的位置,按照"Saturday 4th October 1997"格式輸出時間值。
mysql> select year(curdate())-year(m_birth) as age,dayofyear(m_birth) as days, -> date_format(m_birth,'%W %D %M %Y') as birthDate from member;+------+------+-----------------------+| age | days | birthDate |+------+------+-----------------------+| 49 | 180 | Monday 29th June 1970 |+------+------+-----------------------+1 row in set (0.00 sec)
⑤插入一筆新的記錄,m_FN值為"Samuel",m_LN值為"Greem",m_birth值為系統目前時間,m_info為空。使用last_insert_id查看最後插入的id值。
mysql> insert into member values( null,'Samuel','Green',now(),null);Query OK, 1 row affected (0.04 sec)mysql> select * from member;+------+--------+-------+---------------------+---------+| m_id | m_FN | m_LN | m_birth | m_info |+------+--------+-------+---------------------+---------+| 1 | Halen | Park | 1970-06-29 00:00:00 | GoodMan || 2 | Samuel | Green | 2019-08-20 12:43:23 | NULL |+------+--------+-------+---------------------+---------+3 rows in set (0.00 sec)
可以看到表格中有兩筆記錄,接下來使用last_insert_id()函數查看最後插入的id值,SQL語句如下:
mysql> select last_insert_id();+------------------+| last_insert_id() |+------------------+| 3 |+------------------+1 row in set (0.00 sec)
mysql> select m_birth,case when year(m_birth)<2000 then 'old' -> when year(m_birth)>2000 then 'young' -> else 'not born' end as status from member;+---------------------+--------+| m_birth | status |+---------------------+--------+| 1970-06-29 00:00:00 | old || 2019-08-20 12:43:23 | young |+---------------------+--------+3 rows in set (0.00 sec)
相關免費學習推薦:mysql資料庫(影片)
以上是MySQL練習之3:MySQL函數的使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!