数据库基础查询方法

mysql表格查询方法:

查询:

1.简单查询

select * from Info --查所有数据
select Code,Name from Info --查指定列的数据
select Code as ‘代号‘,Name as ‘姓名‘ from Info --给列指定别名

2.条件查询

select * from Info where Code=‘p001‘
select * from Info where Sex=‘true‘ and Nation=‘n001‘ --多条件并的关系
select * from Info where Sex=‘true‘ or Nation=‘n001‘ --多条件或的关系

3.范围查询

select * from Car where Price>40 and Price<50
select * from Car where Price between 40 and 50

4.离散查询

select * from Car where Code in (‘c001‘,‘c005‘,‘c010‘,‘c015‘)
select * from Car where Code not in (‘c001‘,‘c005‘,‘c010‘,‘c015‘)

5.模糊查询

select * from Car where Name like ‘%宝马%‘ --查包含宝马的
select * from Car where Name like ‘宝马%‘ --查以宝马开头的
select * from Car where Name like ‘%宝马‘ --查以宝马结尾的
select * from Car where Name like ‘宝马‘ --查等于宝马的

select * from Car where Name like ‘__E%‘ --查第三个字符是E的

% 代表是任意多个字符

_ 代表是一个字符

6.排序查询

select * from Car order by Price asc --以价格升序排列
select * from Car order by Price desc --以价格降序排列
select * from Car order by Oil desc,Price asc --以两个字段排序,前面的是主条件后面的是次要条件

7.分页查询

select top 5 * from Car
select top 5 * from Car where Code not in (select top 5 Code from Car)

当前页:page = 2; 每页显示:row = 10;

select top row * from Car where Code not in (select top (page-1)*row Code from Car)

8.去重查询

select distinct Brand from Car

9.分组查询

select Brand from Car group by Brand having count(*)>2

10.聚合函数(统计查询)

select count(*) from Car --查询所有数据条数
select count(Code) from Car --查询所有数据条数

select sum(Price) from Car --求和
select avg(Price) from Car --求平均
select max(Price) from Car --求最大值
select min(Price) from Car --求最小值

高级查询

1.连接查询

select * from Info,Nation --形成笛卡尔积

select * from Info,Nation where Info.Nation = Nation.Code

select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code

select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式

2.联合查询

select Code,Name from Info
union
select Code,Name from Nation

3.子查询

一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。

--查询民族为汉族的所有人员信息
select * from Info where Nation = (select Code from Nation where Name = ‘汉族‘)

(1)无关子查询

子查询可以单独执行,子查询和父查询没有一定的关系

--查询系列是宝马5系的所有汽车信息
select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = ‘宝马5系‘)

(2)相关子查询

--查找油耗低于该系列平均油耗的汽车

select * from Car where Oil<(该系列的平均油耗)
select avg(Oil) from Car where Brand = (该系列)

select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)

时间: 2024-07-30 14:57:25

数据库基础查询方法的相关文章

Django 数据库基本查询方法

基础查询方法 get 查询单一结果,模型类实例,如果不存在会抛出模型类 DoesNotExist 异常 filter 过滤出多个结果,返回 QuerySet 类型对象 exclude 排除掉符合条件剩下的结果,返回 QuerySet 类型对象 all 查询所有结果,返回 QuerySet 类型对象 count 查询结果数量 过滤条件 表达语法如下: 属性名称__运算符=值 语法 条件 id__exact=3 (省略写法: id=3) 查询id=3的数据 name__contains='e' 查询

数据库基本查询方法等

数据库:数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅仅是存储和管理数据,而转变成用户所需要的各种数据管理的方式.数据库有很多种类型,从最简单的存储有各种数据的表格到能够进行海量数据存储的大型数据库系统都在各个方面得到了广泛的应用. 我们简单地学习了数据库的基本的创建方法和简单地查询方法: 创建: create语句: 如: create database f21; create t

各种数据库分页查询方法

具体实现中,根据所用数据库.数据量.实现分页方式,选择分页实现快的方式实现. 一.MYSQL分页查询方法 MYSQL分页查询主要使用其自带的limit函数,但需根据查询量来决定具体的使用方式,如只有几千或几万数据,则直接用 limit m,n方式, 如数据量较多,则要注意limit的使用方式. // limit m , n:从第 m 条数据开始,获取 n 条数据 1.数据量少的方式:select * from tablename limit m,n; // limit m , n:m 可省略,省

数据库分页查询方法

在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法. 可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应用总结归纳一下,以方便大家查询使用. 下面就分别给大家介绍.讲解一下三种数据库实现分页查询的方法. 一. MySQL 数据库分页查询 MySQL数据库实现分页比较简单,提供了LIMIT函数.一般只需要直接写到sql语句后面就行了. LIMIT子句可以用来限制由SELECT语句返回过来的数据数量,它有

SQL数据库的查询方法

简单查询: 一.投影 select * from 表名 select 列1,列2... from 表名 select distinct 列名 from 表名 二.筛选 select top 数字 列|* from 表名 (一)等值与不等值 select * from 表名 where 列名=值 select * from 表名 where 列名!=值 select * from 表名 where 列名>值 select * from 表名 where 列名<值 select * from 表名

Yii2中对数据库的查询方法如下

User::find()->all(); 此方法返回所有数据: User::findOne($id); 此方法返回 主键 id=1 的一条数据(举个例子): User::find()->where(['name' => '小伙儿'])->one(); 此方法返回 ['name' => '小伙儿'] 的一条数据: User::find()->where(['name' => '小伙儿'])->all(); 此方法返回 ['name' => '小伙儿']

数据库的查询方法

简单查询:一.投影select * from 表名select 列1,列2... from 表名select distinct 列名 from 表名 二.筛选select top 数字 列|* from 表名(一)等值与不等值select * from 表名 where 列名=值select * from 表名 where 列名!=值select * from 表名 where 列名>值select * from 表名 where 列名<值select * from 表名 where 列名&g

页面查询某一个时间段的数据的方法或者查询多个条件的数据库的查询方法

1.只查询在某一个时间段的数据 $param = $this->request->post(); $b=array(); if(!empty($param['start_time']) && !empty($param['end_time'])){ $b['create_time']=array('between',strtotime($param['start_time']).','.strtotime($param['end_time']));} $StyleTypeMod

数据库基础查询

--查询年级编号为1的学生记录:INSERT UPDATE DELETE --基本语法:SELECT 列名 FROM 表名 [WHERE 条件表达式] [ORDER BY] SELECT StudentNo,StudentName FROM Student WHERE GradeId=1 --查询学生姓名和年级名称 --查询多个表中数据时:1.明确查询的列   2.明确查询的表 3.WHERE 找表与表之间的共同列(主外键关系) SELECT Student.StudentName,Grade.