mysql--数据的插入、查询、子查询、多表连接查询

一、数据的插入

 按顺序插入到表中的列(常用这个):    insert into 表名 values(value1,value2,value3,........)

 针对特定的列插入:   insert into 表名(column1,column3)values(value 1,value3)

例子:insert into customer values(1,'Lily','女','256 street');

         insert into customer(name,sex) values('Lily','女');

二、数据的查询

查询的5种语句:where (条件查询)/having(筛选)/group by(分组)/order by(排序)/limit(限制结果数)

1.where +condition

conditon:

  • 比较运算符:< 、 >、=、<=、>=、<>、!=
  • 逻辑运算符:and (&&)、or(||)  、not(!)
  • 模糊查询 like   配合任意字符”%“一起使用

例:select * from emp where emp_name="瑞瑞";

      select * from emp where enm_sal>5000;

      select * from emp where sal>5000 and emp_bir<"1988-10-22";

      select emp_name,emp_sex,emp_sal from emp where  sal>5000 and emp_bir<"1988-10-22";(字段查询)

2.group by +group_type

一般情况下group by 和统计函数配合使用。      统计函数:max/min/sum/avg/count

例:select emp_sex,count(*) from emp group by emp_sex;统计男女职工数目。

3.having +where_definition

having是对查询结果中的列发挥作用。

where是对表中的列发挥作用。

where后的表达式怎么写,having后就怎么写。

例:select goods_id,good_name,shop_price as s from goods having s>200;

      select goods_id,good_name from goods where shop_price>200;

4.order by +order_type

order by price;(默认升序排列)

order by price desc;(降序排列)

order by price asc;(升序排列)

order by rand();(随机排列)

例:select * from emp order by emp_sal desc;(工资降序排列)

5.limit +limit_criteria

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

      select * from emp order by emp_sal desc limit 1,2;查看工资排名第二到第三的员工

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

三、子查询

 

 

 

 

posted @ 2015-06-03 17:14  ruirui0525  阅读(933)  评论(0)    收藏  举报