魅力博客

魅力Linux|魅力空间|魅力博客|学习Linux|ubuntu日记|电脑教程|手机软件

SQL的查询语句大全



1.查询所有列


select * from table


2.查询某一列


select username from table


3.查询id=1的数据


select * from table where id = 1


4.查询中文name列


select * from table where name = "内容"


5.查询数字范围


select * from table where id > 3


select * from table where id > 3 and id < 6(等同SELECT  要查找的字段 FROM 表名 WHERE 字段名 BETWEEN 初始值 AND 终止值)


select * from table where id > 3 or id < 2


6.查询指定多条数据


select * from table where id in (1,3,5,7)


select * from table where id not in (2,4,6,8)


7.模糊查询


select * from table where name like "%公共内容%"


select * from table where name not like "%公共内容_"


select * from table where name like "_公共内容_"(前后一个字符不同,公共内容相同的集合。如果是前(后)两个字符不同再加一个_即可)


注意:like 不配合%或_就相当于=


8.查询出集合排序


select * from table order by money(默认正序从大到小)


select * from table order by money desc(倒序从小到大)


9.查询出集合进行操作


select count(*) from table(数据条数)


select sum(money) from table (某列数据的和)


select max(money) from table(某列数据的最大值)


select min(money) from table(某列数据的最小值)


select avg(money) from table(某列数据的平均值)


10.查询出的数据可用substr进行裁剪(name="username")


select substr(name,3) from where id = 1(结果为ername)


select substr(name,2,4) from where id = 1(结果为ser,从第2个开始,截取4个)


11.分组查询出数据


select * from table group by name


select groupid,avg(english) table group by groupid(查询每个小组英语平均分)


12.查询出的结果再放入查询语句


select * from table having sum(money) > 150.0


13.查询出的表取别名


select name as 姓名 , pass as 密码 from table


14.查询出的两张表剔除相同的结果


select name from user union select name from admin


15.查询出的结果小写化lower


select lower(name) from user


16.查询出的结果大写化upper


select upper(name) from user


17.查询出的多列结果拼接


select concat(name,pass) from user等同于s


18.查询两列的值是否相等


select name || pass from user(返回0,1)


19.查询出字段的长度


select length(name) from user


20.查询出的结果转为ASCII值


select ascii("2") name from user


21.剔除相同列的值的数据


select distinct name from stu


15.查询结果join连接



inner join(等值连接) 只返回两个表中联结字段相等的行


left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录


right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录


例:select * from student s inner join course c on s.id = c.id

————————————————

转载自:https://blog.csdn.net/qq_34926773/article/details/77868410

相关文章:Mysql数据库相关操作命令



返回顶部

发表评论:

Powered By Z-BlogPHP 1.7.3


知识共享许可协议
本作品采用知识共享署名 3.0 中国大陆许可协议进行许可。
网站备案号粤ICP备15104741号-1