sql 基础学习

标题申明:sql基础应用入门 (个人学习总结一)

sql对大小写不敏感。

先了解sql的基础语法:select语句基本使用(包括where、and/or 、order by 、top、like、between、通配符

where----有条件的从表中选取数据

and 和 or — 基于一个以上的条件对记录进行过滤

order by —对结果进行排序(默认是升序),若在语句后面添加desc,则是降序

top —规定要返回记录的数目(可以是返回的具体数目 , 也可以是百分比)

like —在where子句中搜索列的指定模式

between—在where子句中使用 ,选取介于两者之间的数据

select distinct—句用于返回唯一不同的值

通配符(%、_ 、[charlist] 、[^charlist]/[!charlist] )—可替代一个或多个字符,必须与like一起使用

以上信息也可直接看这里。
——————————————————————————————————

select * from student where id=1; --查找id等于1的列

select  * from student where id=1 and sex='male'; --查找id等于1且性别为女性的列
select  * from student where id=1 or sex='male'; --查找id等于1或性别为女性的列

select top 3 * from student ;--查找前3行数据
select top 50 percent * from student ;--查找前50%的数据

select * from student  where  name like 'a%';  --查找name是以a开头的所有列
select * from student where name like '%b';  --查找name是以b结尾的所有列
select * from student where  name  like 'a_';  --查找name是以a开头后面只有一个字符 的所有列
select * from student where name like '[ac]%';  --  查找name是以a/c开头的所有列
select * from student  where name like '[^ac]%'; --查找name 不是以a/c开头的所有列

select * from student where id between 2 and 5; --查找id在[2,5]之间的数据

SELECT - 从数据库表中获取数据
UPDATE - 更新数据库表中的数据
DELETE - 从数据库表中删除数据
INSERT INTO - 向数据库表中插入数据

先创建个数据库;
create database websecurity;
在这里插入图片描述
显示出数据库;
show databases;
在这里插入图片描述
选择数据库
use websecrity;
在这里插入图片描述
创建表单;
create table student( id int(4) not null primary key auto_increment, name char(20) not null, sex char(20) nou null, addr char(20) not null ) ;
在这里插入图片描述
显示表单;
show tables;
在这里插入图片描述
由于表单中没有数据,所以我们添加数据;在这里插入图片描述
插入表单信息;
insert into 表单(列1,列2,列3~~)values (值1,值2,值3~~);
insert into student(name,sex,addr) values ('leo','male','hangzhou');
insert into student(name,sex,addr) values ('leoo','male','hangzhou');
insert into student(name,sex,addr) values ('leooo','female','hang');
insert into student(name,sex,addr) values ('leoooo','female','hang');
在这里插入图片描述
再创建个teacher的表单(如上所示)
在这里插入图片描述
在这里插入图片描述
union select(合并表单);//不显示重复数据
union all // 显示重复数据
select * from teacher where sex='male' union all select * from student where sex='male'
在这里插入图片描述
order by(排序);
select * from teacher order by name; select * from teacher order by 2; "2"代表是第二列的意思;
在这里插入图片描述

select * from teacher where sex='male' union all select * from student where sex='male' order by 2; (合并表单后按第二列排序)
在这里插入图片描述

注释;

#zhushi
-- zhushi  

(-- 后面有括号)

跨库查询;
再新建个数据库,方法如上;建立库名为xuexi,表名为xuexi;
select * from 数据库名.表名;
select * from websecurity.student;
在这里插入图片描述

如何让union后命令输出在第一行,只需在让前面的报错,这样就不会显示前面执行令
在这里插入图片描述
where 后面一步是判断,1=1肯定是对的,而1=2是错的,所以报错。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值