Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101350784
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: Mysql/postgreSQL

2008-05-20 22:47:34

来源:

2.5 修改数据

2.5.1 使用UPDATE语句来更新表中的数据
    mysql> update emp set emp_id=100001 where emp_name='红枫';

    修改结果显示如下:

    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100005 | 啸天   |     27 |   3000 | 1979-07-10 | male   |
    | 100001 | 红枫   |     29 |   8000 | 1977-01-01 | male   |
    | 100002 | 丽鹃   |     27 |   7000 | 1979-12-31 | fmale   |
    +--------+----------+---------+---------+------------+---------+
    3 rows in set (0.00 sec)

    如果该语句没有后面的where限制,将会对表中所有的记录都进行修改。
    如,给全部人加薪1000,可以如下修改:
    mysql> update emp set emp_sal=emp_sal+1000;

    修改结果显示如下:

    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100005 | 啸天   |     27 |   4000 | 1979-07-10 | male   |
    | 100001 | 红枫   |     29 |   9000 | 1977-01-01 | male   |
    | 100002 | 丽鹃   |     27 |   8000 | 1979-12-31 | fmale   |
    +--------+----------+---------+---------+------------+---------+
    3 rows in set (0.00 sec)

----------------------我------------割--------------------------

2.6 高级查询

2.6.1 记录查询

    查询emp表中,emp_name为啸天的全部信息
    mysql> select * from emp where emp_name='啸天';

    查询结果显示如下:
    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100005 | 啸天   |     27 |   4000 | 1979-07-10 | male   |
    +--------+----------+---------+---------+------------+---------+
    1 row in set (0.00 sec)

    查询emp表中,emp_sal,工资在5000以上的全部信息
    mysql> select * from emp where emp_sal>5000;

    查询结果显示如下:
    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100001 | 红枫   |     29 |   9000 | 1977-01-01 | male   |
    | 100002 | 丽鹃   |     27 |   8000 | 1979-12-31 | fmale   |
    +--------+----------+---------+---------+------------+---------+
    2 rows in set (0.00 sec)

    查询emp表中在1978年1月1日之后出生的
    mysql> select * from emp where emp_bir>'1978-01-01';

    查询结果显示如下:
    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100005 | 啸天   |     27 |   4000 | 1979-07-10 | male   |
    | 100002 | 丽鹃   |     27 |   8000 | 1979-12-31 | fmale   |
    +--------+----------+---------+---------+------------+---------+
    2 rows in set (0.00 sec)

    查询emp表中在1979年12月1日之前出生,工资在5000以上的
    mysql> select * from emp where emp_bir<'1979-12-01' and emp_sal>5000;

    查询结果显示如下:
    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100001 | 红枫   |     29 |   9000 | 1977-01-01 | male   |
    +--------+----------+---------+---------+------------+---------+
    1 row in set (0.00 sec)

2.6.2 字段查询

    CEO查看工资情况
    mysql> select emp_name,emp_sal from emp;

    查询结果显示如下:
    +----------+---------+
    | emp_name | emp_sal |
    +----------+---------+
    | 啸天   |   4000 |
    | 红枫   |   9000 |
    | 丽鹃   |   8000 |
    +----------+---------+
    3 rows in set (0.00 sec)

    查看1978年后出生的人的姓名、工资和性别
    mysql> select emp_name,emp_sal,emp_sex from emp where emp_bir>"1977-12-31";

    查询结果显示如下:
    +----------+---------+---------+
    | emp_name | emp_sal | emp_sex |
    +----------+---------+---------+
    | 啸天   |   4000 | male   |
    | 丽鹃   |   8000 | fmale   |
    +----------+---------+---------+
    2 rows in set (0.00 sec)

2.6.3 查询结果排序

    用ORDER BY语句对emp表中所有员工工资高低顺序查询结果(默认是从低到高——升序)
    mysql> select * from emp order by emp_sal;

    查询结果显示如下:
    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100005 | 啸天   |     27 |   4000 | 1979-07-10 | male   |
    | 100002 | 丽鹃   |     27 |   8000 | 1979-12-31 | fmale   |
    | 100001 | 红枫   |     29 |   9000 | 1977-01-01 | male   |
    +--------+----------+---------+---------+------------+---------+
    3 rows in set (0.00 sec)

    用DESC关键字来进行从高到低排序——降序
    mysql> select * from emp order by emp_sal desc;

    查询结果显示如下:
    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100001 | 红枫   |     29 |   9000 | 1977-01-01 | male   |
    | 100002 | 丽鹃   |     27 |   8000 | 1979-12-31 | fmale   |
    | 100005 | 啸天   |     27 |   4000 | 1979-07-10 | male   |
    +--------+----------+---------+---------+------------+---------+
    3 rows in set (0.00 sec)

2.6.4 查询结果数量的限制

    用LIMIT查看emp表中工资收入排名前两个员工的资料:
    mysql> select * from emp order by emp_sal desc limit 2;

    查询结果显示如下:
    +--------+----------+---------+---------+------------+---------+
    | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
    +--------+----------+---------+---------+------------+---------+
    | 100001 | 红枫   |     29 |   9000 | 1977-01-01 | male   |
    | 100002 | 丽鹃   |     27 |   8000 | 1979-12-31 | fmale   |
    +--------+----------+---------+---------+------------+---------+
    2 rows in set (0.00 sec)

  查看工资排名第2到第3的员工资料:
  mysql> select * from emp order by emp_sal desc limit 1,2;

  查询结果显示如下:
  +--------+----------+---------+---------+------------+---------+
  | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
  +--------+----------+---------+---------+------------+---------+
  | 100002 | 丽鹃   |     27 |   8000 | 1979-12-31 | fmale   |
  | 100005 | 啸天   |     27 |   4000 | 1979-07-10 | male   |
  +--------+----------+---------+---------+------------+---------+
  2 rows in set (0.01 sec)

  使用rand()抽样调查,随机抽取2个员工,查看其资料
  mysql> select * from emp order by rand() limit 2;

  如如下结果:(随机的)

  +--------+----------+---------+---------+------------+---------+
  | emp_id | emp_name | emp_age | emp_sal | emp_bir   | emp_sex |
  +--------+----------+---------+---------+------------+---------+
  | 100005 | 啸天   |     27 |   4000 | 1979-07-10 | male   |
  | 100001 | 红枫   |     29 |   9000 | 1977-01-01 | male   |
  +--------+----------+---------+---------+------------+---------+
  2 rows in set (0.01 sec)

2.6.5 查询结果的字段联合和重新命名

    mysql> select concat(emp_id," ",emp_name) from emp;

    查询结果:
    +------------------------------+
    | concat(emp_id," ",emp_name) |
    +------------------------------+
    | 100005 啸天           |
    | 100001 红枫           |
    | 100002 丽鹃           |
    +------------------------------+
    3 rows in set (0.00 sec)

阅读(342) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~