标题申明: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是错的,所以报错。