T-sql 各种查询命令

目录

1.选中表中所有的列

2.选中表中指定的列

3.选中表中指定的列(条件查询)

范围查询    (between)

并且查询    (and)

或查询        (or)

in 查询        (in)

返回行数     (top n)

4. LIKE 模糊匹配

5.查询并 去重

6.查询并 排序

7.查询列 并且改名称

8.查询并插入(新表)

9.查询结果 插入其他表 (表以存在)


1.选中表中所有的列

select * from 表名 

例子:

select * from sun                                        输出sun表中所有的列

2.选中表中指定的列

select 列1,列2.... from 表名 

select name,age.... from sun                输出sun表中name age列的所有内容 

3.选中表中指定的列(条件查询)

select 列1,列2.... from 表名  where 条件 

例子:

select name,age.... from sun    where name=‘小明’         

输入表中小明的 名字和年龄

范围查询    (between)

select * from 表 where age between 13 and 20   

输出 年龄在13到20之间的人所有信息

并且查询    (and)

都要满足条件

select * from 表 where age=‘18’ and adress=‘北京’ 

 输出年龄18 并且在北京人的信息

或查询        (or)

满足一点即可

select * from 表 where 工资=6000 or 奖金=1000 

输出 工资等于6000或者奖金等于1000的信息

in 查询        (in)

select * from 表 where 数学 in  (80 ,90,100)

输出 数学成绩等于80 90 100的所有人信息

返回行数     (top n)

n表示需要返回的行数

4. LIKE 模糊匹配

like %             

%` 表示零个或多个字符的通配符。

select * from 表 where name like ‘王%’

查找出姓王的同学

like _

下划线通配符 `_`

select * from 表 where name like ‘王_ _’

查找出姓王某某的同学  

5.查询并 去重

select DISTINCT 列1,列2 .... from 表名  

6.查询并 排序

select 列1,列2..... from 表 order by  age desc        (asc| desc)升序 降序

7.查询列 并且改名称

select name AS 名字, age AS 年龄  from 表  

8.查询并插入(新表)

select  name,age,address  into 新表1    from 表2

将从表2 查询出的 name age adress列 信息 插入到   新表1之中

9.查询结果 插入其他表 (表以存在)

insert into 表2 (列1,列2...)select 列 from 表1

你可能感兴趣的:(sql,数据库)