查询 DML

查询 DML:select DQL

语法(一部分)
select *,字段名,表达式,函数调用,
from 表名
where 查询的条件 匹配记录

--查询出Book表中bid为'A0001'的信息:
--bid bname pub字段
select bid,bname,pub --投影操作
from Book
where bid = 'A0001';--选择操作
--查询book表中所有信息
--所有行、所有列
--所有行:不写where没有条件 都匹配
--所有列:*
select * from Book
--查询出bid为‘C0001’的图书信息
--提示:最多一行 所有列
select * from Book 
where bid='C0001';
--查询book,给查询的字段起别名
--语法: as别名 可以省略关键字as
select bname  as 书名,author1 as 作者,pub 出版社 
from Book
--查询结果的拼接
--将多个列合并为一个列显示 +
select bname as 书名,
       author1+' '+author2 as 作者
from Book;
--java语法:表达式
--author1+""+author2
--select后可加常量
use worker
select name,salary,'试用'as 类型
from worker
--select 之后可以跟 *,字段名,常量,函数调用
select * from worker
select 1+2 from worker  --有几行 就算几次
select 1+2;
select GETDATE() as 当前系统;
select '试用' as 类型;


--查询指定条件的记录 where子句
--选择 过滤出需要的行(记录)
--查询出学生表中姓名为张三的所有信息
use student
go
select *
from studentinfo
select *
from studentinfo
where 姓名='张三';  --等值比较
/*使用比较运算符表示各种条件
 =  >  <  >=  <=  !>  !<  
 不等于 !=  <> 
*/
-- 查询出顾客表中性别为女的所有客户的cid cname address sex信息
use Library
select * from Customer
select cid,cname,address,sex
from Customer
where sex='女';
select * from Book where numinput>50;
select * from Book where numinput!>50;
select * from Book where numinput!=40;
select * from Book where numinput<>30;
--查询出不住北京海淀的顾客
--查询出 姓名 住址
select cname,address
from Customer
where address !='北京海淀';
/*
空值 null 带来的影响
结论:空值和任何值包括null都无法比较,比较后都返回假
判断是否为空: is null
判断是否不为空:is not null
*/
select * from Customer where address is null;

--逻辑表达式连接词
-- and 
-- or
-- not
--查询出住在北京海淀并且性别为男的顾客信息
select *
from Customer
where address='北京海淀'and sex='男'
--查询出库存量在10到35之间的图书信息
--
select *
from Book
where numstore>=10 and numstore<=35;
-- where numstore between 10 and 35; 闭区间
--between m and n 在[m,n]之间 闭区间
in(m,n,k,...)只要出现在括号中,返回真
-- 查询出bid为‘B0001’或'B0002'的tushuxinxi息
select * from Book
where bid ='B0001'or bid = 'B0002';
--等价于
select * from Book
where bid in ('B0001','B0002');
/*
模糊查询
语法:字段名 like '匹配字符串'
匹配字符串:
% 表示任意多个字符
_ 任意一个
[] 在范围内的一个字符
[^]不在范围内的一个字符
*/
--已知bid一共有5个字符
--查询出bid以‘B000’开头的图书信息
select *
from Book
where bid like 'B000_';
 --[]只能表示一个字符
 --表示字符范围ABC开头,后面任意
 Select * from Book where bid  like '[A-C]%';
 select * from Book where bid  like '[ABD]%';
 select * from Book where bid  like '[A-CN]%';
 select * from Book where bid  like '[^ADN]%';
 /*
 关于select的技巧:使用select可以复制表
 语法:
 select * into 新表名 from 源表名;
 会根据源表的结构创建新表
 同时将查询回的结果插入到新表中
 */
 use Library
 GO 
 sp_help Book
 select * from Book
 --使用Book表复制一张新的表book1
 select * into book1 from Book;
 sp_help book1
 select * from book1
 --复制时可以指定条件,指定行、列
 select bid,bname,pub,author1
 into book2
 from Book
 where pub = '人民出版社';
 go
 sp_help book2
 select * from book2
 
 --创建新表book3,只拷贝表的结构
 --不拷贝数据
 select *  into  book3 from Book
 where 1<>1; --永假式  技巧
 sp_help book3
 select * from book3
  • 分离和附加数据库

1、分离数据库
将某个数据库脱离管理器管理
注意:不能是当前正在使用的数据库
2、 附加数据库
将某个数据库加入到管理器管理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值