Mysql数据库-使用的查询语句大全

  • Post author:
  • Post category:mysql


查询所有字段:select * from 表名;

查询指定字段:select 列1,列2,… from 表名;

使用 as 给字段起别名: select 字段 as 名字…. from 表名;

查询某个表的某个字段:select 表名.字段 …. from 表名;

可以通过 as 给表起别名: select 别名.字段 …. from 表名 as 别名;

消除重复行:distinct 字段



1.


查询一张表:


select * from


表名;



2.


查询指定字段:


select


字段


1


,字段


2


,字段


3….from


表名;



3.

where



条件查询:


select


字段


1


,字段


2


,字段


3  from


表名


where


条件表达式;



例:


select * from t_studect where id=1;



select * from t_student where age>22;



4.






in



关键字查询:


select


字段


1


,字段


2  from


表名


where


字段


[not]in(


元素


1


,元素


2)






例:


select * from t_student where age in (21,23);



select * from t_student where age not in (21,23);



5.






between and



的范围查询:


select


字段


1


,字段


2  from


表名


where


字段


[not]between


取值


1 and


取值


2






例:


select * from  t_student where age between 21 and 29;



select * from  t_student where age not between 21 and 29;



6.






like



的模糊查询:


select


字段


1


,字段


2… from


表名


where


字段


[not] like ‘


字符串









“%”


代表任意字符;



“_”


代表单个字符;



例:


select * from  t_student where stuName like ‘


张三









select * from  t_student where stuName like ‘


张三


%”






select * from  t_student where stuName like ‘%


张三


%”





//


含有张三的任意字符



select * frome t_student where stuName like ‘


张三


_”






7.



空值



查询:


select


字段


1


,字段


2… from


表名


where


字段


is[not] null;



8.






and





多条件

查询:



select


字段


1


,字段


2… from


表名


where


条件表达式


1 and


条件表达式


2 [and


条件表达式


n]



例:


select * from t_student where gradeName=’


一年级


’ and age=23






9.






or



的多条件查询



select


字段


1


,字段


2… from


表名


where


条件表达式


1 or


条件表达式


2 [or


条件表达式


n]



例:


select * from t_student where gradeName=’


一年级


’ or age=23





//


或者,条件只要满足一个



10.

distinct



去重复查询:


select distinct


字段名


from


表名;



11.


对查询结果排序



order by






select


字段


1


,字段


2…from


表名


order by


属性名


[asc|desc]



例:


select * from  t_student order by age desc





//


降序,从大到小



select * from  t_student order by age asc





//


升序,


asc


默认可以不写



12.


分组查询



group by




group by


属性名


[having


条件表达式


][with rollup]



13.limit


分页查询:


select


字段


1


,字段


2





…from


表名


limit


初始位置



例:


select * from t_student limit 0,5






1.


内连接查询(两张或以上的表连接起来查询需要的数据)



根据表一的


bookTypeId


查询出所有


bookTypeName



select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id



查询某几个字段:



select bookNme,author from t_book,t_bookType where t_book.bookTypeId=t_bookType.id;



2.




外连接查询(




两张或以上的表连接起来




查询某张表的信息)



3.


左连接查询


:select * fromt_book

left  join

t_bookType

on




t_book.bookTypeId = t_bookType.id;



4.


右连接查询



select * from t_book

right join

t_bookType

on

t_book.bookTypeId=t_bookType.id;



5.


多条件连接查询



select * from t_book,t_bookType where t_book.bookTypeId=t_bookType.id

and

t_book.price>70;



子查询



1.






in



关键字的子查询(一个查询语句的条件可能落在另一个


select


语句的查询结果中)



select * from t_book where bookType

in

(select id from t_bookType);



select * from t_book where bookType

not in

(select id from t_bookType);



2.


带比较运算符的子查询(子查询可以使用比较运算符)



select * from t_book where price

>=

(select price from t_priceLevel where priceLevel=1);



3.





exists


关键字的子查询(加入子查询查询到记录,则进行外层查询,否则,不执行外层查询)



select * from t_book where

exists

(select * from t_booktype);



select * from t_book where

not exists

(select * from t_booktype);



4.





any


关键字的子查询(


any


关键字表示满足其中任一条件)



select * from t_book where price >= any(select price from t_priceLevel);



5.





all


关键字的子查询(


all


关键字表示满足所有条件)



select * from t_book where price >= all(select price from t_priceLevel);



合并查询



1.union



使用


union


关键字是,数据库系统会将所有的查询结果合并到一起,然后去掉相同的记录;



select id from t_book

union

select id from t_bookType;



2.union all



使用


union all


,不会去除掉重复的记录;



select id from t_book

union all

select id from t_bookType;



版权声明:本文为Mr_GGI原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。