案例:使用各种函数 *** 作数据,掌握各种函数的作用和使用方法。
(1)使用数学函数rand()生成3个10以内的随机整数。
(2)使用sin(),con(),tan(),cot()函数计算三角函数值,并将计算结果转换成整数值。
(3)创建表,并使用字符串和日期函数对字段值进行 *** 作。
(4)使用case进行条件判断,如果m_birth
小于2000年,就显示"old";如果m_birth大于2000年,则显示"young"。
(免费学习推荐:mysql视频教程)
(1)、使用数学函数rand()生成3个10以内的随机整数。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)(2)、使用sin(),con(),tan(),cot()函数计算三角函数值,并将计算结果转换成整数值。
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)(3)、创建表,并使用字符串和日期函数对字段值进行 *** 作。
①创建表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)(4)、使用case进行条件判断,如果m_birth小于2000年,就显示"old";如果m_birth大于2000年,则显示"young"。
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函数的使用的详细内容,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)