收藏文章 楼主

数据库基础通用--增删改查

版块:mysql 数据库通用基础大全   类型:精华/置顶   作者:小绿叶技术博客   查看:3897   回复:0   获赞:1   时间:2019-08-14 16:50:23

查询---select


select * from dbo._Ls;

#查询在dbo._ls表中的信息


select Base from dbo._Ls;

#查询表中的Base字段


select distinct Base from dbo._Ls;

#查询表中的Base字段,不重复的数据


select count(*) from Lot

--使用count(*) 查询lot表所有内容有多少行数据


#========================带有条件的查询================

语法 :  select 列名称 from 表名称  where 列 运算符 值
(运算符有=、<=、!=、>=、<、>、<>、like、between)



查询指定年份每月的总量 

select year(a.CreateDate) as 年, -- 使用 yesr() 函数获取年份

month(a.CreateDate) as 月, -- 使用 month() 获取月份

count(a.WorkcenterName) as 数量 -- 计算要统计的列,(总数太多,下面的group by 分组统计对应的月份的数量)

from dbo.Workcenter a -- 从哪个表里面

where year(a.CreateDate)=2017 -- 指定年份

group by year(a.CreateDate),month(a.CreateDate)

-- 需要进行分组的列

每个月的总数






翻译

select 选择 [sɪˈlekt] ---C lei k t 

where 在哪里 [weə(r)] --wei ou



select name,number,type

from dbo.spt_values

where number=10

--查询name,number,type这三个字段,从dbo.spt_values表里面去找,查询字段number=10的内容(查询有几行)

--where number<10 将最后一行换成这个,将查询在number字段中小于10的内容

--where LotSN like 'L%'  查询像F的字符的字段,也可用where LotSN=L 来精确查找

--where  LotSN like 'LDLQG%1812%' 前面加百分号表示前面有数据,再来百分号表示之前还有数据

--where  LotSN like 'LDLQG%1812%'  order by Qty desc 表示匹配LotSN字段数据后,将Qty字段降序排列升降序只能是数字


--查询字段指定内容(是空的字段内容):where PriorityLevel is null


select * from Lot

where LotId='LOT100019A1I' and LotSN='LDLQG-1812001200001'

--匹配符合两个字段指定字符的内容

--将and  改成 OR 为或则,任意一个条件成立就匹配


select * from Lot

where CreateDate between '2019-8-9' and '2019-8-10'

#查询lot表,从时间列CreateDate 中找,where 条件 赛选  and区间between,  当天:9号之后10号之前



=========  案例  ==========


select * from Chay_Temp A

where A.field03 between '2019-05-27' and '2019-05-28'

order by A.field03 desc;

查询这个时间段的内容,将这个表倒序(desc)排列,正序为asc

查询最后一笔写入的数据

select top 1*  from lot

order by CreateDate DESC

lot最后一笔写入的数据在哪个号里面,从lot表中在CreateDate


两表关联查询 

select top 10 * from ProductRoot

where ProductName='03-1-022-1520725'


select b.FirstSN,b.SecondSN 

from  ProductRoot a, SNRelationShip b  

where a.ProductName='03-1-022-1520725' 

and b.ProductId=a.DefaultProductId 

and b.CreateDate between '2019-5-1' and '2019-6-1'

and SecondSN != ''


-- 先在a表里面查一下记录编码是哪个列,ProductRoot是管理编码的列

-- 选择b表的两个字段,这是我们要的输出结果

-- 从这两个表里,并建立别名,a表 和 b表

-- 在a表中的 ProductName 字段里面找编对应一行中的 参数

-- 将a表中的 ProductName 匹配字段结果中 对应的ProductRootid列中的参数,传给b表ProductId列进行下一步匹配

-- between and 在什么之间函数,

-- 字段不等于空字符




二、INSERT 插入数据

创建表eisc

create table eisc(

id int,

name varchar(225),

password varchar(255),

xingqi varchar(255),

city varchar(255)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

                                                  // ENGINE=InnoDB 作用: 下次可以直接读取二进制数据表,而不会锁表


alter table eisc add column id int(20); 

// alter [ˈɔltər] 改变 table 表; column [ˈkɑləm] 纵行; add 增加一个 id 列

ALTER TABLE userinfoExamine drop column id;

// 删除一列 名为id

插入多行数据数据

insert into eisc(id,name,password,xingqi,city)

values('4','ddd','qqqqq','星期一','沙发'),

('7','ddd','qqqqq','星期一','沙发'),

('8','ddd','qqqqq','星期一','沙发')

#插入多行数据,不需要加values


UPDATE(更改数据)

更新多个数据

update eisc

set name='asfdasfsadfdfa',password='9999999'

where name='aaa'


-- 在eisc表中,去指定name='aaa'的数据,修改name为:'asfdasfsadfdfa',修改password='9999999'  取消逗号和后面的内容将变成只修改单个内容




update www

set name=replace(name,'e','w') -- 使用replace()函数替换字符

where id like '1%'

select * from www


-- 批量将字符串替换为指定字符串

-- 有效语句,已验证可行


DELETE删除


DELETE FROM `eisc` WHERE `eisc`.`id` = 12                      # 删除数据库 表 eisc 中的 字段 id 为 12   


delete

from eisc

where id=1

#删除,从eisc表中,找到ID=1的所有数据


delete

from eisc 

#删除数据库中所有数据



drop table eisc

-- 删除表


-- 将drop 换成 create ,表示为创建表


drop database eisc

-- 删除数据库


delete 清空表

truncate table table_name;

delete * from table_name;

注 : truncate 快速删除,delete 逐条删除





数据库高级语句:

https://www.eisc.cn/index.php?c=read&id=113&page=1

本文章最后由 admin2024-08-08 10:35 编辑

提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:数据库基础通用--增删改查

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息