Mysql  查询语句大全
\1.两表之间的查询,例如:查询员工表中部门号与部门表中部门号相等
select * from tb_emp ,tb_dept  where tb_emp.deptno=tb_dept.deptno;
(这是同时显示两张表中相等的depton 所有字段)(tb_emp ,tb_dept这都是表名)
2.select  tb_e.deptno from tb_e, tb_d where tb_e.deptno=tb_d.deptno;
(这是只显示员工表中的tb_e.deptno,并且条件是员工表中部门号与部门表中部门号相等)
3.给字段取别名
select product_price*12 totsl_product_price from productinfo;
  等价select product_price*12  from productinfo;
也可以这样写 select product_price*12 " totsl product_price" from productinfo;
有特殊的字符时用双引号的方法,(特殊字符是:中文,日文,分号等)(totsl product_price是 product_price*12)
****** 0 和空 还有空格不是一个概念
例如:
select * from emp where description  is null;
select * from emp where description  =0;
select * from emp where description  ='空格';
查询的结果都市不一样的。
distinct 关键字可以查询结果中清除重复的行,他的作用范围是后面的所有字段的组合;
例如:
  select distinct deptno ,deptname from emp where deptno=23;
totsl_product_price是product_price的别名;
select ename, sal*12 as '年薪'from emp;  别名的作用是 让查询出来的结果可以让别人(外行)看了一目了然的作用
上面是针对产品价格计算出总金额,价格*12,也就是对字段值进行加减乘除,
*****任何含有空值的表达式计算后的值都是空值。( 空值+20=空值,等等)
不等值查询(mysql两者都支持)
select * from productinfo where product_id!=33;
oracl的不等值查询
select * from productinfo where product_id<>'33';
小于查询
select * from productinfo where product_id<'33';
大于查询
select * from productinfo where product_id>'33';
等于查询
select * from productinfo where product_id='33';
在一定值范围内查询 例如  1000--5000的查询
select ename, sal as '月薪'from emp where sal>=1000 and sal<=5000;
  在两个值之间的查询    and (包含最小值和最大值)
select ename, sal as '月薪'from emp  where  sal between  1000 and 5000;
  in  查询  在什么里面
示例 :在emp表中查询部门编号为25 25 20 这三个值中的 某些字段
select ename, sal,deptno from emp where  deptno in (25,26,20);
  not in 刚好与上面的相反
  like  是做模糊查询使用
and  表示要满足所有条件 例如:
select ename, sal,deptno from emp where ename like '%ff' and deptname='市场部';
or  查询  表示只要满足其中一个条件就行,例如
select ename, sal,deptno from emp where ename like '%ff' or deptname='市场部';
  not in  取反的意思  表示不包含
  优先级 级别 (可以用括号提高优先级别)
 
 
  排序  用的是  order by
  降序  desc 
  默认是升序
  注意  排序时  排序的字段只能是int 类型  否则排序的效果会出现不理想的结果
*/*****可以在这样做  sal是varchar  对sal排序时:
select ename as '姓名', sal as'月薪' from emp order by sal+0;
mysql group by order by
例如:自己建表的时候,把一个字段类型创建为varchar(2) ,其实应该建为int(2)的。因为我只允许输出数字。这本来也没什么,无非就是占点空间,懒得改了。但是今天在后台发现排序有问题。于是,没办法,改之。下面简单说一下MySQL的varchar排序问题,引以为戒。
下面,我从数据库里面以server_id排一下序,大家来看一下排序后的结果:
select server_id from cardserver where game_id = 1 order by server_id desc limit 10;
+-----------+
| server_id |
+-----------+
| 8         | 
| 7         | 
| 6         | 
| 5         | 
| 4         | 
| 3         | 
| 2         | 
| 10        | 
| 1         | 
+-----------+
 
很明显,我想要的结果应该是 10,8,7,6,5 这样的。但是这个10排在了2的后面。按照字符串来排的。其实我是想把它当做数值来排。
手动转换类型:
用下面的方法就可以了,使server_id+0之后再排序,问题解决了。
select server_id from cardserver where game_id = 1 order by server_id+0 desc limit 10;
+-----------+
| server_id |
+-----------+
| 10        | 
| 8         | 
| 7         | 
| 6         | 
| 5         | 
| 4         | 
| 3         | 
| 2         | 
| 1         | 
+-----------+
 
使用MySQL函数CAST/CONVERT:
mysql为我们提供了两个类型转换函数:CAST和CONVERT,现成的东西我们怎能放过?
CAST() 和CONVERT() 函数可用来获取一个类型的值,并产生另一个类型的值。
这个类型 可以是以下值其中的 一个:
BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
所以我们也可以用CAST解决问题:
select server_id from cardserver where game_id = 1 order by CAST(server_id as SIGNED) desc limit 10;
也可以使用CONVERT来搞定此问题:
select server_id from cardserver where game_id = 1 order by CONVERT(server_id,SIGNED) desc limit 10;
在productinfo表中查询 update_time ,create_time这两个字段,条件是product_id='33';
select  update_time ,create_time from productinfo where product_id='33';
  在两个值之间的查询    and
select ename, sal as '月薪'from emp  where  sal between  1000 and 5000;
插入数据时一定要注意字段的类型  int型不用加单引号,varchar 型的要加上单引号
例如:
insert into emp  (deptno,ename ,sex, eemail ,deptname ,sal) values (30,'luo1','nan','123456','开发部','3000');
auto_increment 自动增长
description 描述,
修改列的类型
alter table 表名 modify 列名 列类型
修改列名
alter table 表名 change 旧列名 新列名  列类型
alter table emp change name ename varchar(20);
增加列
  alter table 表名 add  column 字段名(列名) 列类型  (column  可以有和无 标准的应该有 column )
alter table emp add  column moblie int (11);
alter table 表名 add  column 字段名(列名)  列类型  after/befor 列名(字段名)
删除列  把上面的add改为drop就行了;
  修改表名 alter table 表名 rename 新表名;
  alter table emp rename  tb_emp;