数据库笔记6:检索,排序检索,过滤数据

简介:

insert into products(prod_id,prod_name,pro_price)values('avno1','.5 ton anvil',5.99);
insert into products(prod_id,prod_name,pro_price)values('avno2','1 ton anvil',9.99);
insert into products(prod_id,prod_name,pro_price)values('avno3','2 ton anvil',14.99);
insert into products(prod_id,prod_name,pro_price)values('ol1','oil can',8.99);
insert into products(prod_id,prod_name,pro_price)values('fu1','fuses',3.42);
insert into products(prod_id,prod_name,pro_price)values('slite','sling',4.49);
insert into products(prod_id,prod_name,pro_price)values('tnt1','tnt (1 stick)',2.50);
insert into products(prod_id,prod_name,pro_price)values('tnt2','tnt (5 stick)',10.00);
insert into products(prod_id,prod_name,pro_price)values('fb','bird seed',10.00);
insert into products(prod_id,prod_name,pro_price)values('fc','carrots',2.50);
insert into products(prod_id,prod_name,pro_price)values('safe','safe',50.00);
insert into products(prod_id,prod_name,pro_price)values('dtntr','detonator',13.00);
insert into products(prod_id,prod_name,pro_price)values('jp1000','jetpack 1000',35.00);
insert into products(prod_id,prod_name,pro_price)values('jp2000','jetpack 2000',55.00);




-- 创建products表
create table products
(
 prod_id char(40),
 prod_name char(40) not null,
 pro_price int not null,
 primary key(prod_id)
)engine = innodb;

-- 检索单个列
select prod_name from products;
-- 检索多个列
select prod_id,prod_name,prod_price from products;
-- 检索所有列
select * from products;
-- 返回所有指定的行
select ven_id from ven;
-- 返回行中不同的值
select     distinct vend_id from ven;
-- 返回开始到第五行
select prod_name from products limit 5;
-- 返回从第五行开始的五行
select prod_name from products limit 5,5;
-- 从第3行开始取4行
select prod_name from products limit 4 offset 3;
-- 假设products表在ven表里面
select products.prod_name from ven.products;
-- 升序
select prod_name,pro_price,prod_id from products order by prod_name,pro_price;
-- 降序
select prod_name,pro_price,prod_id from products order by pro_price desc;
-- desc之前的降序之后的升序
select prod_name,pro_price,prod_id from products order by pro_price desc,prod_name;
-- 检索降序并输出一行
select pro_price,prod_id from products order by pro_price desc limit 1;
-- 检索两个列并判断prod_price为3的行
select prod_name from products where prod_price = 3;
-- 检索两个列并判断prod_name为fuses的行,不区分字母大小写
select prod_name,pro_price from products where prod_name ='Fuses';
-- 检索两个列并判断prod_price小于10的行
select prod_name,pro_price from products where pro_price < 10;
-- 检索两个列并判断prod_price小于等于10的行
select prod_name,pro_price from products where pro_price <= 10;
-- 检索三列并prod_id判断不等于jp1000的行
select  prod_id,prod_name,pro_price from products where prod_id <> 'jp1000';
-- 与上面相同
select  prod_id,prod_name,pro_price from products where prod_id != 'jp1000';
-- 检索pro_price 5到10之间的所有产品
select  prod_name,pro_price from products where pro_price between 5 and 10;
-- 检索pro_price为空的所有产品
select  prod_id,prod_name,pro_price from products where pro_price is null;
-- 创建customers表
create table customers
(
 cust_id int,
 cust_email char(50) null,
 primary key(cust_id)
)engine = innodb;

-- 向customers表cust_id列插入数据
insert into customers(cust_id)values(1002);
-- 向customers表cust_id列插入数据
insert into customers(cust_id)values(1003);
-- 检索cust_email为空的所有产品
select  cust_id from customers where cust_email is null;

create table ven
(
 ven_id int auto_increment,
 primary key(ven_id)
)engine = innodb;
-- 添加vend_id列
alter table ven add vend_id int;
insert into ven(vend_id)values(1001);
insert into ven(vend_id)values(1001);
insert into ven(vend_id)values(1001);
insert into ven(vend_id)values(1002);
insert into ven(vend_id)values(1002);
insert into ven(vend_id)values(1003);
insert into ven(vend_id)values(1003);
insert into ven(vend_id)values(1003);
insert into ven(vend_id)values(1003);
insert into ven(vend_id)values(1003);
insert into ven(vend_id)values(1003);
insert into ven(vend_id)values(1003);
insert into ven(vend_id)values(1005);
insert into ven(vend_id)values(1005);


深入理解:

select * from student;

------------------------------------------
select 
 t.sid+1 'tsid加了1',
 t.sid+5,
 t.score+100,
 t.sid+t.score as 'sid_score',
 t.*,
 5*8 '五'

 from student t;

------------------------------------------

select

 t.sid,

 t.sname,

-- 设置别名

 from student t;

------------------------------------------

select 

 t.sid,

 t.sname,

-- 常量列

 5,

from student t;

------------------------------------------

select 

-- sid列数据加一,并创建一个t.sid+1列的数据

 t.sid +1,

 t.sname,

 t*

 from student t;

------------------------------------------

select

-- 两个整型数据相加,得出t.score+t.ccid列名的相加数据

 t.score+t.ccid

 from student t;

------------------------------------------

select

-- 'ni'列名

 t.score 'ni',

-- 'why'列名

 t.ccid 'why',

-- 'you'列名

 t.score+t.ccid 'you'

 from student t;

------------------------------------------

select

-- 整型和字符类型数据相加,得出t.score+t.sname列名的t.score数据(字符类型和整型数据相加,结果也是一样,以整型为主)

 t.score+t.sname

 from student t;

------------------------------------------

select

-- 改为'我'列名

 t.score '我'

 from student t;

------------------------------------------

select

-- '加'列名

 t.score '加',

-- ‘王’列名

 5 ‘王’

 from student t;

------------------------------------------

select

-- '加'列名

 t.score '加',

 5 

 from student t

-- 常数列名不能相加(还是‘加’列名数据,5列名数据)

 where t.score +5;

 

------------------------------------------

select s.score+5 'q',s.score+s.ccid,5 '我' from student s;

------------------------------------------

select 

-- '加'列名

 t.ccid '加',

-- 常数列名

 5 ,

-- t.ccid+5列名相加的数据
 t.ccid + 5
from student t;

------------------------------------------

-- 

select * from student t where t.score>70 order by t.score desc limit 5;
------------------------------------------
select s.*,1 'a',2 'b',3 'c' from student s  
 where s.score+1=s.sid+87 and s.score between 60 and 95
  order by sid desc limit 1;
------------------------------------------

select * from student t
 where t.score not in (60,90,85);

------------------------------------------
select * from student 
 where sname is not null;

------------------------------------------

select * from student;

------------------------------------------

create table student

(
 id int not null auto_increment,
 primary key(id)
)engine = innodb;
------------------------------------------
alter table student add sname char(40);
alter table student add score int;
alter table student add ccid int;

------------------------------------------

insert into student(sname,score,ccid) values('wpq2',93,1);

insert into student(sname,score,ccid) values('wpq3',45,1);
insert into student(sname,score,ccid) values('QQ5',43,1);
insert into student(sname,score,ccid) values('wp9',60,1);
insert into student(sname,score,ccid) values('w',99,1);
insert into student(sname,score,ccid) values('w123',91,1);

本文转自    风雨萧条 博客,原文链接:   http://blog.51cto.com/1095221645/1433344     如需转载请自行联系原作者
相关文章
|
28天前
|
数据采集 关系型数据库 MySQL
python爬取数据存入数据库
Python爬虫结合Scrapy与SQLAlchemy,实现高效数据采集并存入MySQL/PostgreSQL/SQLite。通过ORM映射、连接池优化与批量提交,支持百万级数据高速写入,具备良好的可扩展性与稳定性。
|
1月前
|
人工智能 Java 关系型数据库
使用数据连接池进行数据库操作
使用数据连接池进行数据库操作
72 11
|
2月前
|
存储 数据管理 数据库
数据字典是什么?和数据库、数据仓库有什么关系?
在数据处理中,你是否常困惑于字段含义、指标计算或数据来源?数据字典正是解答这些问题的关键工具,它清晰定义数据的名称、类型、来源、计算方式等,服务于开发者、分析师和数据管理者。本文详解数据字典的定义、组成及其与数据库、数据仓库的关系,助你夯实数据基础。
数据字典是什么?和数据库、数据仓库有什么关系?
|
2月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL数据库的WAL日志与数据写入的过程
PostgreSQL中的WAL(预写日志)是保证数据完整性的关键技术。在数据修改前,系统会先将日志写入WAL,确保宕机时可通过日志恢复数据。它减少了磁盘I/O,提升了性能,并支持手动切换日志文件。WAL文件默认存储在pg_wal目录下,采用16进制命名规则。此外,PostgreSQL提供pg_waldump工具解析日志内容。
216 0
|
1月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
83 3
|
1月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
2月前
|
存储 运维 关系型数据库
从MySQL到云数据库,数据库迁移真的有必要吗?
本文探讨了企业在业务增长背景下,是否应从 MySQL 迁移至云数据库的决策问题。分析了 MySQL 的优势与瓶颈,对比了云数据库在存储计算分离、自动化运维、多负载支持等方面的优势,并提出判断迁移必要性的五个关键问题及实施路径,帮助企业理性决策并落地迁移方案。
|
1月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
1月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。

热门文章

最新文章