sql查询基本语法

发布 2021-05-11 05:27:28 阅读 6883

1、计算列。

select * from emp

--*表示所有的。

--from emp 表示从emp表查询。

select empno,ename from emp;

select ename,sal*12 as "年薪"from emp

--as可以省略,记住“年薪“不要写成‘年薪’,也不要写成年薪,方便移植。

select ename,sal*12 as "年薪",sal "月薪",job from emp

select 5 from emp;

--ok ,输出一个常量值。

--输出的行数是emp表的行数,每行只有一个字段,值是5

select 5;

--ok--不建议使用,没什么实际意义。

注意:在oracle中字段的别名不允许用单引号括起来。

但是sqlserver 2005中却允许。

因此为了兼容性,最好字段别名用双引号括起来,不要用单引号。

2、distinct【不允许重复的】

selectdistinct deptno from emp; -distinct deptno 会过滤掉重复的deptno值。

selectdistinct commfrom emp; -distinct可以过滤掉重复的null,或者说如果有多个null,会把一个输出。

selectdistinct comm,deptnofrom emp; -distinct把comm和deptno的组合进行过滤,查出两两互不相同的记录,select distinct deptno,comm from emp

-含义:把deptno和comm的组合都不重复的输出。

select deptno,distinct comm from emp --error,deptno的元素多comm中的元素少。

select deptno,distinct comm from emperror,逻辑上有冲突。

学习方式:要多思考。

在某个范围之内】

-查找工资在1500到3000之间(包括1500到3000)的所有员工的信息。

select * from emp where sal between 1500 and 3000;

-等价于。select * from emp where sal>=1500 and sal<=3000;

-查找工资小于1500或者大于3000(包括1500到3000)的所有员工的信息。

select * from emp where sal<1500 or sal>3000;

select * from emp where sal not between 1500 and 3000;

属于若干个孤立值】

-把sal=1500 和sal=3000的元素输出。

select * from emp where salin(1500,3000);

-等价于。select * from emp where sal=1500 or sal=3000

-把sal不等于1500 和sal不等于3000的元素输出。

select * from emp where salnot in(1500,3000);

-等价于。select * from emp where sal!=1500 and sal!=3000

select * from emp where sal<>1500 and sal<>3000

-数据库中不等于有两种表示:!=建议使用第二种。

--多或取反是并且非并且取反是或。

5、top:【最前面的若干条诗句】可用于分页。

select top 2 * from emp; -筛选出前两行数据。

select top 2 from emp; -error

select top 15 percent * from emp; -如果百分比不是整数则向上取整。

-把工资在1500到3000 之间(包括1500和300)的员工中工资最高的前4个人的信息输出。

selecttop4 * from empwheresalbetween 1500 and 3000order by sal

-执行过程 from哪个表->where->order排序->top->所有字节。

-desc 降序,不写则默认是升序(asc)

6、null【没有值,空值】

①、零和人null是不一样的,null表示空值,没有值,零表示一个确定的值。

②、null 不能参与如下的运算:<>

③、null可以参与如下的运算: is not is

-输出奖金非空的员工的信息-

select * from emp where comm <>null; -输出为空。

select * from emp where comm !=null; -输出为空。

select * from emp where comm=null; -输出为空error

--总结:null不参与<> 运算。

-null可以参与is not is

select * from emp where comm is null; -输出奖金为空的员工的信息。

select * from emp where comm is not null; -输出奖金不为空的员工的信息。

④、任何类型的数据都允许是null

create table t1(

name nvarchar(20),cnt int,riqi datetime

insert t1 values(null,null,null);

select * from t1;

⑤、任何数字与null参与数**算的结果永远是null

-输出每个员工的姓名年薪(包含奖金) comm假设是一年的奖金。

select empno, ename,sal*12+comm "年薪" from emp;

-此程序证明,任何数字与null参与数**算的结果永远是null

-正确的写法是:

select empno, ename,sal*12+isnull(comm,0) "年薪" from emp;

-isnull(comm,0)如果comm是null 就返回零,否则返回comm的值。

7、order by【以某个字段排序。

order by a,ba和b都是升序。

order by a,b desc --a升序,降序。

order by a desc,b --a降序,b升序。

order by a desc ,b desc --a降序,b降序。

文字描述。如果不指定排序的标准,则默认是升序,升序用asc表示,默认可以不写。

为一个字段指定的排序标准并不会对另外一个字段产生影响。

强烈建议为每一个字段都指定排序的标准。

例子:-asc是升序的意思,默认可以不写,desc是降序。

select * from emp order by sal --默认是按照升序排序。

select * from emp orderby deptno,sal;--先按照deptno升序排序,如果deptno相同,再按照sal升序排序。

select * from emp orderby deptnodesc,sal ;

--先按照deptno降序排序,如果deptno相同,再按照sal升序排序,--记住sal是升序,不是降序。

--order by a desc,b,c,d desc 只对a产生影响, 不会对后面的b、c、d产生影响。

select * from emp order by deptno,sal desc;

-问题:desc是否会对deptno产生影响?

-答案:不会、

-先按deptno升序,如果deptno相同,再按sal降序。

8、模糊查询。

格式:select 字段的集合 from 表名 where 某个字段的名字 like匹配的内容。

匹配的条件通常含有通配符。

表示任意0个或者多个字符。

select * from emp where ename like '%a%';ename 只要含有字符a就输出。

select * from emp where ename like 'a%';ename 只要首字母是a的就输出。

select * from emp where ename like '%a'; ename 只要未字母是a的就输出。

[这是下划线不是减号]

表示单个字符。

select * from emp where ename like '_a';-ename只要第二个字母是a的就输出。

a-f]a到f中的任意单个字符,只能是a b c d e f中的任意一个。

select * from emp where ename like '_a-f]%'

-把ename中第二个字母是a或b或c或d或e或f的数据输出。

a,f]a或f

^a-c]不是a,也不是b,也不是c的任意单个字符。

select * from emp where ename like '_a-f]%'

SQL基本语法

创建 sql语言中的createtable语句被用来建立新的数据库 create table语句的使用格式如下 create table tablename column1 data type,column2 data type,column3 data type 现举例如下 create tabl...

SQL基本语法

select top 正整數 distinct all 欄位。from 資料表,where條件式 group by 欄位,h ing條件式 order by 欄位 desc asc limit start,count 內的代表不一定要有,但在select之後的欄位顯示,如果沒有設定欄位,則一定要填上...

基本的查询语法

select all distinct top expression percent with ties select list into new table from n where group by all group by expression n with h ing search cond...