查询语句格式

 1 SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [HIGH_PRIORITY]  
 2 [DISTINCT | DISTINCTROW | ALL]  
 3 select_expression,...  
 4 [INTO {OUTFILE | DUMPFILE} 'file_name' export_options]  
 5 [FROM table_references  
 6 [WHERE where_definition]  
 7 [GROUP BY col_name,...]  
 8 [HAVING where_definition]  
 9 [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...]  
10 [LIMIT [offset,] rows]  
11 [PROCEDURE procedure_name] ] 

1.查询所有数据:

select * from table;

2.查询行:

select * from table where (字段名1=‘xx’ and 字段名2!='xx' )  or 字段名3>100;

3.查询列:

select 字段1,字段2,字段3... from table;

4.查询排序行:

正序:select * from table order by 字段名1,字段名2;(可按多字段优先级排序)

倒序:select * from table order by 字段名1,字段名2 desc;(可按多字段优先级排序)

5.日期计算:(日期函数)

 1 mysql> select * from user;
 2 +----+------+----------+------+------+---------------------+---------------------+
 4 | Id | name | nikename | sex  | age  | birth_date          | goodbye_date 5  |
 6 +----+------+----------+------+------+---------------------+---------------------+
 8 |  1 | yql  | sunshine |    1 |   28 | 1988-10-20 10:00:00 | NULL 9  |
10 |  2 | Sam  | Sam      |    0 |   25 | 1989-10-20 10:00:00 | 2005-03-20 10:00:0011  |
12 +----+------+----------+------+------+---------------------+---------------------+

 

 1 插入一条记录:
 2 mysql> insert into user (id,name,nikename,sex,age,birth_date) values(1,'yql','sunshine',1,28,'1989-10-20 10:00:00')
 3 查看计算结果:to_days(now())天数
 4 mysql> select  name,(to_days(now())-to_days(birth_date))/365 as age1 from user;
 5 +------+---------+
 6 | name | age1    |
 7 +------+---------+
 8 | yql  | 26.9863 |
 9 +------+---------+
10 1 row in set (0.06 sec)

 

posted @ 2015-10-09 18:48  sunshine-habit  阅读(369)  评论(0编辑  收藏  举报