简单SQL查询

--【指定数据库】
use demo
go

--【1】查询表的所有数据
--【语法】select * from 表名
select * from hanshu


--【2】查询表中的列
--【语法】select 列名1,列名2,列名3..... from 表名
select name from hanshu

--【3】查询表中列名为女的数据,或者是id=1,或者是add='湖南益阳'
--【语法】select * from 表名 where sex='女'
select * from hanshu where sex='女'

--【4】查询表中name字段包含汤的数据
--【语法】select * from 表名 where 列名 like '汤%'
select * from hanshu where name like '汤%'

--【5】查询表字段moeny中5到12之间的数据
--【语法】select * from 表名 where 列名 between 数字 and 数字
select * from hanshu where moeny between 5 and 12

--【6】查询表字段moeny中2.5.20数据
--【语法】select * from 表名 where 列名 in(2,5,20)
select * from hanshu where moeny in(2,5,20)

--【7】求表中moeny的平均值
--【语法】select avg(列名) from 表名
select avg(moeny) from hanshu ;

--【8】求表中moeny的总和
--【语法】select sum(列名) from 表名
select sum(moeny) from hanshu ;

--【9】求表中字段moeny小于20的数据
--【语法】select * from 表名 where 字段名<20
select * from hanshu where moeny<20

原文地址:https://www.cnblogs.com/tangtangsimida/p/9557651.html