mysql 单表 not in_MySQL 单表查询必知必会

摘要:

下文讲述MySQL数据库中,单表查询你必须知道的事情,您可以通过学习下面的文章,做到彻底的掌握MySQL单表查询,如下所示:

实验环境:sql server 2008 R2

通过本文的学习你可以达到的目的:

熟练掌握MySQL的select和where关键字的用法。

--切换数据库

---创建测试数据表

create table userInfo(

keyId int unsigned primary key auto_increment not null,

name varchar(80) default "",

age tinyint unsigned default 0,

height decimal(6,2),

gender enum ("男", "女", "未填写") default "未填写",

group_id int unsigned default 1,

is_delete bit default 0

);

-- 班级表

create table groupInfo(

group_id int not null,

name varchar(80) not null

);

-- 插入测试数据, 都是偶像, 没有其他意思哈.

insert into userInfo

(name,age,height,gender,group_id,is_delete)

values

(0,'余昌砥',16,160.00,1,1,0),

(0,'卞炳根',19,180.00,2,2,0),

(0,'罗登铬',23,195.00,1,1,0),

(0,'岑步抱',45,135.00,1,2,0),

(0,'孔芬百',80,180.00,2,1,0)

insert into groupInfo values

(1, "第一小组"),

(2, "第二小组");

--单表select查询测试

mysql> select * from userInfo;

mysql> select * from groupInfo;

--查询前2行数据方法字段

mysql> select * from userInfo limt 2;

--查询指定字段的前2行数据的方法

mysql>select name, age from userInfo limit 2;

--as输出列别名

mysql>select name as "名字", height as "身高"

from userInfo

where keyId in (1,2);

--as 返回结果集取别名

mysql>select name as "名字", height as "身高"

from userInfo as test

where keyId in (1,2);

--distinct 过滤重复行

mysql>select distinct gender

from userInfo ;

/*

where 条件检索数据分享

-- 年龄大于或等于160

select *

from userInfo

where height >=160;

-- 身高大于150的信息

select *

from userInfo

where height > 150;

--身高等于150的信息

select * from userInfo where height = 150;

-- 身高不等于150的信息

select *

from userInfo

where height !=150;

/*

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

逻辑运算符: and, or, not

*/

-- 身高在150-170间的用户信息

select *

from userInfo

where (height >= 150) and (height <= 170);

-- 身高小于150的男生

select *

from userInfo

where (height < 150) and (gender = "男");

-- or

-- 身高超过170 或者 年龄在30以上的 男生 姓名和群组Id

select name, group_id as "群组Id"

from userInfo

where ((height > 170) or (age > 30))

and (gender = "男");

-- not

-- 不在 身高170以上的女生 姓名和身高

select name, height

from userInfo

where not (height >= 170 and gender = "女");

/*

null 空值检测

*/

-- 身高未填写的人

select name, age, height

from userInfo

where height is null;

-- 身高填写的人

select name, age, height

from students

where height is not null;

范围查询 in; between...and

/*

in 关键字的应用

*/

----18 ,19 ,20岁的人的信息

select *

from userInfo

where age in (18, 19, 20)

---between..and 用法

----年龄在 18-30之间的所有用户

select *

from userInfo

where age between 18 and 30

;

---like关键字的用法

-- % 替换任意个; _ 替换1个, _ _ 替换2个;

-- 姓名中,以"孔"开头的所有信息

select *

from userInfo

where name like "孔%";

-- 姓名中, 带有"孔"的所有名字

select *

from userInfo

where name like "%孔%";

-- 姓名只有2个字长度的信息

-- 使用两个下划线占位符

select *

from userInfo

where name like "__";

-- 姓名最至少为2个字的信息

select *

from userInfo

where name like "__%";

-- 名字的第二个字是 "孔王"

select *

from userInfo

where name regexp ".孔.*";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值