大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> 数据库 -> MySQL入门学习(四)学习篇(2)

MySQL入门学习(四)学习篇(2)

时间: 2021-07-31 作者:daque

上篇咱们学会了怎样创造一个数据库和数据库表,并领会怎样向数据库表中增添记载。 那么咱们怎样从数据库表中检索数据呢? 1、从数据库表中检索消息 本质上,前方咱们仍旧用到了select语句,它用来从数据库表中检索消息。 select语句方法普遍为: select 检索要害词 from 被检索的表 where 检索前提(可选) 往日所运用的“ * ”表白采用一切的列。 底下连接运用咱们在上篇作品中创造的表mytable: 2、查问一切数据: mysql> select * from mytable; +----------+------+------------+----------+ | name | sex | birth | birthaddr | +----------+------+------------+--------+ | abccs|f| 1977-07-07 | china | | mary |f| 1978-12-12 | usa | | tom |m| 1970-09-02 | usa | +----------+------+------------+----------+ 3 row in set (0.00 sec) 3、矫正缺点记载: 假设tom的出华诞期有缺点,该当是1973-09-02,则不妨用update语句来矫正: mysql> update mytable set birth = "1973-09-02" where name = "tom"; 再用第22中学的语句看看能否已矫正过来。 4、采用一定行 上头窜改了tom的出华诞期,咱们不妨采用tom这一条龙来看看能否仍旧有了变革: mysql> select * from mytable where name = "tom"; +--------+------+------------+------------+ | name |sex | birth | birthaddr | +--------+------+------------+------------+ | tom|m| 1973-09-02 | usa| +--------+------+------------+------------+ 1 row in set (0.06 sec) 上头where的参数指定了检索前提。咱们还不妨用拉拢前提来举行查问: mysql> select * from mytable where sex = "f" and birthaddr = "china"; +--------+------+------------+------------+ | name |sex | birth | birthaddr | +--------+------+------------+------------+ | abccs |f| 1977-07-07 | china | +--------+------+------------+------------+ 1 row in set (0.06 sec) 5、 采用一定列 假设你想察看表中的一切人的全名,则不妨如许操纵: mysql> select name from mytable; +----------+ | name | +----------+ | abccs | | mary | | tom | +----------+ 3 row in set (0.00 sec) 即使想列出全名和性别两列,则不妨用逗点将要害词name和birth划分: myaql> select name,birth from mytable; 6、对前进行排序 咱们不妨对表中的记载按华诞巨细举行排序: mysql> select name, birth from mytable order by birth; +----------+------------+ | name | birth | +----------+------------+ | tom | 1973-09-02 | | abccs| 1977-07-07 | | mary | 1978-12-12 | +----------+------------+ 3 row in set (0.00 sec) 咱们不妨用desc来举行逆序排序: mysql> select name, birth from mytable order by birth desc; +----------+------------+ | name | birth | +----------+------------+ | mary | 1978-12-12 | | abccs| 1977-07-07 | | tom | 1973-09-02 | +----------+------------+ 3 row in set (0.00 sec) 7、 行计数 数据库常常要统计少许数据,如表中职工的数量,咱们就要用到行计数因变量count()。 count()因变量用来对非null截止的记载举行计数: mysql> select count(*) from mytable; +----------+ | count(*) | +----------+ |3 | +----------+ 1 row in set (0.06 sec) 职工中士女数目: mysql> select sex, count(*) from mytable group by sex; +------+----------+ | sex | count(*) | +------+----------+ | f|2 | | m|1 | +------+----------+ 2 row in set (0.00 sec) 提防咱们运用了group by对sex举行了分批。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.