MSSQL

MSYQL数据操作语言(DML)

日期:2015-06-28 00:00:00 来源: IT猫扑网

  一、select语法

  SELECT

  [ALL | DISTINCT | DISTINCTROW ]

  [HIGH_PRIORITY]

  [STRAIGHT_JOIN]

  [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]

  [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]

  select_expr [, select_expr ...]

  [FROM table_references

  [WHERE where_condition]

  [GROUP BY {col_name | expr | position}

  [ASC | DESC], ... [WITH ROLLUP]]

  [HAVING where_condition]

  [ORDER BY {col_name | expr | position}

  [ASC | DESC], ...]

  [LIMIT {[offset,] row_count | row_count OFFSET offset}]

  [PROCEDURE procedure_name(argument_list)]

  [INTO OUTFILE 'file_name'

  [CHARACTER SET charset_name]

  export_options

  | INTO DUMPFILE 'file_name'

  | INTO var_name [, var_name]]

  [FOR UPDATE | LOCK IN SHARE MODE]]

  简化一下语法如下:

  select  column1,column2,...

  from   table1,table2,...

  [where condition]

  [group by …]

  [having …]

  [order by …]

  1、例如:下面例子

  mysql> create table jokes(

  -> id int unsigned not null auto_increment primary key,

  -> joketext text,

  -> jokedate date

  -> )engine=myisam charset=utf8;

  mysql> insert into jokes values('','why not?',now());

  #计算列(select 用于检索从一个或多个表中选取出的行。Select也可以被用于检索没有引用任何表的计算列)

  select 1+1 as total;  //结果为2

  +-------+

  | total |

  +-------+

  |     2 |

  +-------+

  #从表Jokes中挑选所有的东西

  select * from  jokes;

  +----+----------+------------+

  | id | joketext | jokedate   |

  +----+----------+------------+

  |  1 | why not? | 2010-04-08 |

  +----+----------+------------+

  #选择感兴趣的列(投影)

  select id, jokedate  from  jokes;

  #我们是不是可以多少显示一点笑话正文的内容呢?(预览)

  mysql> select id,left(joketext,3) as content,jokedate from jokes;

  +----+---------+------------+

  | id | content | jokedate   |

  +----+---------+------------+

  |  1 | why     | 2010-04-08 |

  +----+---------+------------+

  #统计表中记录数

  mysql> select count(*) as records from jokes;

  +---------+

  | records |

  +---------+

  | 1 |

  +---------+

  #统计2000年1月1日之后录入的笑话记录数

  select  count(*)  from  jokes where  jokedate >= ‘2000-01-01’;

  #笑话里包含"鞋"  //%表示任一字符

  mysql> select joketext as content from jokes where joketext like '%not%';

  +----------+

  | content  |

  +----------+

  | why not? |

  +----------+

  #显示2000年4月份的包含"鞋"的笑话(组合查询)

  select  joketext  from  where  joketext like "%not%" and  jokedate >= "2000-04-01"  and jokedate < "2000-05-1";

  2、分组语句

  员工表emp(empno, name, tel, deptno, sal)

  部门表dept(deptno, dname, addr)

  1)显示每个部门的最高工资

  select deptno, max(sal) from emp  group by deptno;

  select deptno,max(sal) as ‘最高工资’ from emp group by deptno;

  注意:max()、min()…函数在显示列中出现时,必须与group by一起使用

  3、嵌套子查询

  1)显示每个部门收入最高的职工信息。

  select *  from emp

  where sal in ( select max(sal) from emp

  group by deptno );(先按组排序,后显示一组中最大的)

  2)按照工资高低显示职工信息

  select  *  from  emp  order by sal desc;

  3)limit s,n

  limit子句可以被用于强制select语句返回指定的记录数。limit接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二参数指定返回记录行的最大数目n。初始记录行的偏移量是0(而不是1)。

  (1)查看薪水最低的前5位员工信息

  select * from emp order by sal limit 0,5;

  óselect * from emp order by sal limit 5;

  (2)查看第3到第7号员工信息

  select * from emp limit 2,5;

相关文章

相关下载

网友评论

我要评论...
    没有更早的评论了
    取消