设为首页 加入收藏

TOP

搞定linux上MySQL编程(二):MySQL语法基础操作(二)
2015-11-21 02:01:30 来源: 作者: 【 】 浏览:1
Tags:搞定 linux MySQL 编程 语法 基础 操作
4-1-17'); Query OK, 1 row affected (0.00 sec) mysql> select * from regusers; +--------+----------+------------+------------+ | userid | username | birth | last_login | +--------+----------+------------+------------+ | 10000 | allen | 1981-01-01 | 2014-02-02 | | 10001 | brooks | 1982-04-02 | 2014-04-30 | | 10002 | curry | 1985-08-12 | 2014-01-17 | +--------+----------+------------+------------+ 3 rows in set (0.00 sec) mysql> 4. 更新记录,使用update语句更新或修改满足规定条件的记录。update语句格式为:update <表名> set <列名>=<表达式> [,<列名>=<表达式>]... [where <条件>];例如将regusers表中姓名为brooks的记录的记录修改为ben,如下:
mysql> update regusers set username='ben' where username='brooks';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from regusers;
+--------+----------+------------+------------+
| userid | username | birth      | last_login |
+--------+----------+------------+------------+
|  10000 | allen    | 1981-01-01 | 2014-02-02 |
|  10001 | ben      | 1982-04-02 | 2014-04-30 |
|  10002 | curry    | 1985-08-12 | 2014-01-17 |
+--------+----------+------------+------------+
3 rows in set (0.01 sec)

mysql> 
5. 删除记录。使用delete语句删除数据表中满足指定条件的记录。delete语句的语法为:delete from <表名> [where <条件>]; 例如删除表中userid为10002的的记录如下:
mysql> delete from regusers where userid=10002;
Query OK, 1 row affected (0.02 sec)

mysql> select * from regusers;
+--------+----------+------------+------------+
| userid | username | birth      | last_login |
+--------+----------+------------+------------+
|  10000 | allen    | 1981-01-01 | 2014-02-02 |
|  10001 | ben      | 1982-04-02 | 2014-04-30 |
+--------+----------+------------+------------+
2 rows in set (0.00 sec)

mysql> 
数据库查询操作:sql使用select语句实现数据库的查询。查询是数据库使用最为频繁的操作。select可以查询表中指定满足条件的若干列,同时可对查询结果按一定顺序排序。
查询表tb_users中所有记录sql如下:
 
 
mysql> select * from tb_users;  
+--------+----------+------------+------------+  
| userid | username | birth      | last_login |  
+--------+----------+------------+------------+  
|  10000 | allen    | 1981-01-01 | 2014-02-02 |  
|  10001 | ben      | 1982-04-02 | 2014-04-30 |  
|  10002 | curry    | 1985-08-12 | 2014-01-17 |  
+--------+----------+------------+------------+  
3 rows in set (0.00 sec)  
查询出生年月小于1983-1-1的所有用户:
 
 
mysql> select * from tb_users where birth < '1983-1-1';  
+--------+----------+------------+------------+  
| userid | username | birth      | last_login |  
+--------+----------+------------+------------+  
|  10000 | allen    | 1981-01-01 | 2014-02-02 |  
|  10001 | ben      | 1982-04-02 | 2014-04-30 |  
+--------+----------+------------+------------+  
2 rows in set (0.00 sec)  
查询出生年月大于1982-1-1,并且按最后登录时间排序:
 
 
mysql> select * from tb_users where birth > '1982-1-1' order by last_login;  
+--------+----------+------------+------------+  
| userid | username | birth      | last_login |  
+--------+----------+------------+------------+  
|  10002 | curry    | 1985-08-12 | 2014-01-17 |  
|  10001 | ben      | 1982-04-02 | 2014-04-30 |  
+--------+----------+------------+------------+  
2 rows in set (0.00 sec)  
只显示用户及其最后登录时间:
 
 
mysql> select username,last_login from tb_users;  
+----------+------------+  
| username | last_login |  
+----------+------------+  
| allen    | 2014-02-02 |  
| ben      | 2014-04-30 |  
| curry    | 2014-01-17 |  
+----------+------------+  
3 rows in set (0.00 sec)

 
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mysqlconcat连接函数 下一篇MySQL更新中文列:1366Incorrects..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: