❶ mysql SQL查询一个字段不是数字开头 如何写
NOT REGEXP '^[a_z]' 用正则匹配首字母
❷ 常见的99中sql语句
1. select * from emp;
2. select empno, ename, job from emp;
3. select empno编号, ename姓名, job工作from emp;
4. select job from emp;
5. select distinct job from emp;
6. select distinct empno, job from emp;
说明:因为雇员编号不重复,所以此时证明所有的列没有重复,所以不能消除掉重复的列.
7.查询出雇员的编号,姓名,工作,但是显示的格式:编号是: 7369的雇员,姓名是: smith,工作是: clear
select '编号是: ' || empno || '的雇员,姓名是: ' || ename || ',工作是: ' || job from emp;
8.求出每个雇员的姓名及年薪
select ename, sal * 12 income from emp;
9.求出工资大于1500的所有雇员信息
select * from emp where sal > 1500;
10.查询每月可以得到奖金的雇员信息
select * from emp where comm is not null;
11.查询没有奖金的雇员信息
select * from emp where comm is null;
12.查询出基本工资大于1500同时可以领取奖金的雇员信息
select * from emp where sal > 1500 and comm is not null;
13.查询出基本工资大于1500或者可以领取奖金的雇员信息
select * from emp where sal > 1500 or comm is not null;
14.查询出基本工资不大于1500或者不可以领取奖金的雇员信息
select * from emp where not(sal > 1500 and comm is not null);
15.查询基本工资大于1500,但是小于3000的全部雇员信息
select * from emp where sal > 1500 and sal < 3000;
16.查询基本工资大于等于1500,但是小于等于3000的全部雇员信息
select * from emp where sal >= 1500 and sal <= 3000;
select * from emp where sal between 1500 and 3000;
17.查询出在1981年雇佣的全部雇员信息(1981年1月1日 到1981年12月31日之间的雇佣的雇员)
select * from emp where hiredate between '1-1月-81' and '31-12月-81';
18.要求查询出姓名是smith的雇员信息
select * from emp where ename = 'SMITH';
19.要求查询出雇员是7369, 7499, 7521的雇员的具体信息
select * from emp where empno = 7369 or empno = 7499 or empno = 7521;
select * from emp where empno in(7369, 7499, 7521);
20.要求查询出雇员不是7369, 7499, 7521的雇员的具体信息
select * from emp where empno not in(7369, 7499, 7521);
21.要求查询出姓名是smith, allen, king的雇员信息
select * from emp where ename in('SMITH', 'ALLEN', 'KING');
22.查询出所有雇员姓名中第二个字母包含"M"的雇员信息
select * from emp where ename like '_M%';
23.查询出雇员姓名中包含字母M的雇员信息
select * from emp where ename like '%M%';
24.要求查询出在1981年雇佣的雇员信息
select * from emp where hiredate like '%81%';
25.查询工资中包含5的雇员信息
select * from emp where sal like '%5%';
26.查询雇员编号不是7369的雇员信息
select * from emp where empno != 7369;
select * from emp where empno <> 7369;
27.要求按照工资由低到高排序
select * frm emp order by sal;
select * from emp order by sal asc;
28.要求按照工资由高到低排序
select * from emp order by sal desc;
29.要求查询出20部门的所有雇员信息,查询的信息按照工资由高到低排序,如果工资相等,则按照雇佣日期由早到晚排序.
select * from emp where deptno = 20 order by sal desc, hiredate asc;
30.将小写字母变为大写字母
select upper('hello') from al;
31.将大写字母变为小写字母
select lower('HELLO WORLD') from al;
32.要求查询出姓名是smith的雇员信息
select * from emp where ename = upper('smith');
33.使用initcap()函数将单词的第一个字母大写
select initcap('hello world') from al;
34.将雇员表中的雇员姓名变为开头字母大写
select initcap(ename) from emp;
35.将字符串"hello"和"world"进行串联
select concat('hello ', 'world') from al;
36.对字符串进行操作的常用字符处理函数
select substr('hello', 1, 3)截取字符串, length('hello')字符串的长度, replace('hello', 'l', 'x')字符串替换from al;
select substr('hello', 0, 3)截取字符串, length('hello')字符串的长度, replace('hello', 'l', 'x')字符串替换from al;
37.显示所有雇员的姓名及姓名的后三个字符
select ename, substr(ename, length(ename) -2) from emp;
select ename, substr(ename, -3, 3) from emp;
38.使用数值函数执行四舍五入操作
select round(789.536) from al;
39.要求将789.536数值保留两位小数
select round(789.536, 2) from al;
40.要求将789.536数值中的整数的十位进行四舍五入进位
select round(789.536, -2) from al;
41.采用trunc()函数不会保留任何小数,而且小数点也不会执行四舍五入的操作
select trunc(789.536) from al;
42.通过trunc()也可以指定小数点的保留位数
select trunc(789.536, 2) from al;
43.作用负数表示位数
select trunc(789.536, -2) from al;
44.使用mod()函数可以进行取余的操作
select mod(10, 3) from al;
45.显示10部门雇员进入公司的星期数(当前日期-雇佣日期=天数/ 7 =星期数)
select empno, ename, round((sysdate - hiredate) / 7) from emp where deptno = 10;
46.日期函数
months_between():求出给定日期范围的月数
add_months():在指定的日期上加上指定的月数,求出之后的日期
next_day():指定日期的下一个日期
last_day():求出给定日期当月的最后一天日期
47.
select empno, ename, months_between(sysdate, hiredate) from emp;
select empno, ename, round(months_between(sysdate, hiredate)) from emp;
48. select sysdate, add_months(sysdate, 4) from al;
49. select next_day(sysdate, '星期一') from al;
50. select last_day(sysdate) from al;
51.转换函数
to_char():转换成字符串
to_number():转换成数字
to_date():转换成日期
52.查询所有雇员的雇员编号,姓名,雇佣日期
select empno,
ename,
to_char(hiredate, 'yyyy') year,
to_char(hiredate, 'mm') months,
to_char(hiredate, 'dd') day
from emp;
select empno, ename, to_char(hiredate, 'yyyy-mm-dd') from emp;
select empno, ename, to_char(hiredate, 'fmyyyy-mm-dd') from emp;
53.查询所有雇员的编号,姓名和工资
select empno, ename, sal from emp;
select empno, ename, to_char(sal, '99,999') from emp;
select empno, ename, to_char(sal, 'L99,999') from emp;
select empno, ename, to_char(sal, '$99,999') from emp;
54. select to_number('123') + to_number('123') from al;
55.将一个字符串转换成日期类型
select to_date('2009-01-01', 'yyyy-mm-dd') from al;
56.求出每个雇员的年薪(要求加上奖金)
select empno, ename, sal, comm, (sal + comm) * 12 from emp;
select empno, ename, sal, comm, nvl(comm, 0), (sal + nvl(comm, 0)) * 12 income from emp;
57. decode()函数类似于if....elsif...else语句
select decode(1, 1, '内容是1', 2, '内容是2', 3, '内容是3') from al;
58.查询出雇员的编号,姓名,雇佣日期及工作,要求将雇员的工作替换成以下信息:
select empno雇员编号,
ename雇员姓名,
hiredate雇佣日期,
decode(job,
'CLERK', '业务员',
'SALESMAN', '销售人员',
'MANAGER', '经理',
'ANALYST', '分析员',
'PRESIDENT', '总裁'
)职位
from emp;
59.笛卡尔积(交差连接)
select * from emp, dept;
select * from emp cross join dept;
60.内连接
select * from emp e, dept d where e.deptno = d.deptno;
select * from emp e inner join dept d on e.deptno = d.deptno;
select * from emp e join dept d on e.deptno = d.deptno;
61.自然连接
select * from emp natural join dept;
select * from emp e join dept d using(deptno);
62.要求查询出雇员的编号,姓名,部门的编号,名称,地址
select e.empno, e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno = d.deptno;
63.要求查询出雇员的姓名,工作,雇员的直接上级领导姓名
select e.ename, e.job, m.ename from emp e, emp m where e.mgr = m.empno;
64.要求查询出雇员的姓名,工作,雇员的直接上级领导姓名以及部门名称
select e.ename, e.job, m.ename, d.dname from emp e, emp m, dept d where e.mgr = m.empno and e.deptno = d.deptno;
65.要求查询出每个雇员的姓名,工资,部门名称,工资在公司的等级(salgrade),及其领导的姓名及工资所在公司的等级
select e.ename, e.sal, d.dname, s.grade, m.ename, m.sal, ms.grade
from emp e, dept d, salgrade s, emp m, salgrade ms
where e.deptno = d.deptno
and e.sal between s.losal and s.hisal
and e.mgr = m.empno
and m.sal between ms.losal and ms.hisal;
select e.ename,
e.sal,
d.dname,
decode(s.grade, 1, '第五等级', 2, '第四等级', 3, '第三等级', 4, '第二等级', 5, '第一等级'),
m.ename,
m.sal,
decode(ms.grade, 1, '第五等级', 2, '第四等级', 3, '第三等级', 4, '第二等级', 5, '第一等级')
from emp e, dept d, salgrade s, emp m, salgrade ms
where e.deptno = d.deptno and e.sal between s.losal and s.hisal and e.mgr = m.empno
and m.sal between ms.losal and ms.hisal;
66. select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e inner join dept d on e.deptno = d.deptno;
67.左外连接
select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno = d.deptno(+);
select empno, ename, d.deptno, dname, loc from emp e left outer join dept d on e.deptno = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e left join dept d on e.deptno = d.deptno(+);
68.右外连接
select empno, ename, d.deptno, dname, loc from emp e, dept d where e.deptno(+) = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e right outer join dept d on e.deptno = d.deptno;
select empno, ename, d.deptno, dname, loc from emp e right join dept d on e.deptno = d.deptno;
69. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno;
70. select e.empno, e.ename, m.empno, m.ename from emp e, emp m where e.mgr = m.empno(+);
71.
select * from emp e, dept d where e.deptno = d.deptno and d.deptno = 30;
select * from emp e inner join dept d on e.deptno = d.deptno where d.deptno = 30;
select * from emp e join dept d on e.deptno = d.deptno where d.deptno = 30;
select * from emp e natural join dept d where deptno = 30;
select * from emp e join dept d using(deptno) where deptno = 30;
72.
select e.ename, d.deptno, d.dname, d.loc from emp e right outer join dept d on e.deptno = d.deptno;
select e.ename, d.deptno, d.dname, d.loc from emp e right join dept d on e.deptno = d.deptno;
select e.ename, d.deptno, d.dname, d.loc from emp e, dept d where e.deptno(+) = d.deptno;
73. select count(ename) from emp;
74. select min(sal) from emp;
75. select max(sal) from emp;
76. select sum(sal) from emp;
77. select avg(sal) from emp;
78. select sum(sal) from emp where deptno = 20;
79. select avg(sal) from emp where deptno = 20;
80.求出每个部门的雇员数量
select deptno, count(deptno) from emp group by deptno;
select deptno, count(empno) from emp group by deptno;
81.求出每个部门的平均工资
select deptno, avg(sal) from emp group by deptno;
82.按部门分组,并显示部门的名称,及每个部门的员工数
select d.dname, count(e.empno) from emp e, dept d
where e.deptno = d.deptno
group by d.dname;
select d.deptno, d.dname, temp.c
from (select deptno, count(e.empno) c from emp e group by e.deptno) temp, dept d
where temp.deptno = d.deptno;
83.要求显示出平均工资大于2000的部门编号和平均工资
select deptno, avg(sal) from emp group by deptno having avg(sal) > 2000;
84.显示非销售人员工作名称以及从事同一工作雇员的月工资的总和,并且要满足从事同一工作的雇员的月工资合计大于5000,输出结果按月工资的合计升序排序.
select job, sum(sal) su from emp where job <> 'SALESMAN' group by job having sum(sal) > 5000 order by su;
select temp.job, sum(temp.sal) s
from (select job, sal from emp e where job <> 'SALESMAN') temp
group by temp.job
having sum(temp.sal) > 5000
order by s;
85.求出平均工资最高的部门工资
select max(avg(sal)) from emp group by deptno;
86.要求查询出比雇员编号为7654工资高的所有雇员信息
select * from emp where sal >(select sal from emp where empno = 7654);
87.要求查询出工资比7654高,同时与7788从事相同工作的全部雇员信息
select * from emp
where sal >(select sal from emp where empno = 7654)
and job = (select job from emp where empno = 7788);
88.要求查询出工资最低的雇员姓名,工作,工资
select ename, job, sal from emp where sal = (select min(sal) from emp);
89.要求查询出:部门名称,部门的员工数,部门的平均工资,部门的最低收入雇员的姓名
select d.dname, temp.c, temp.a, e.ename
from dept d,
(select deptno, count(empno) c, avg(sal) a, min(sal) m from emp group by deptno) temp,
emp e
where d.deptno = temp.deptno and e.sal = temp.m;
select d.deptno, temp.dname, temp.c, temp.a, e.ename, e.sal
from
(select d.dname , count(e.empno) c, avg(e.sal) a, min(e.sal) m
from emp e, dept d
where e.deptno = d.deptno
group by d.dname) temp,
emp e,
dept d
where temp.m = e.sal
and temp.dname = d.dname;
90.求出每个部门的最低工资的雇员的信息
select * from emp where sal in(select min(sal) from emp group by deptno);
select * from emp where sal =any(select min(sal) from emp group by deptno);
select * from
(select min(sal) m from emp group by deptno) temp,
emp e
where e.sal = temp.m;
91.范例90中,比子查询条件中最低(小)的工资要大的雇员信息
select * from emp where sal >any(select min(sal) from emp group by deptno);
select * from emp where sal > (select min(min(sal)) from emp group by deptno);
92.范例90中,比子查询条件中最高(大)的工资要小的雇员信息
select * from emp where sal
select * from emp where sal < (select max(min(sal)) from emp group by deptno);
93.范例90中,比子查询条件中最高(大)的工资要大的雇员信息
select * from emp where sal >all(select min(sal) from emp group by deptno);
select * from emp where sal > (select max(min(sal)) from emp group by deptno);
94.范例90中,比子查询条件中最低(小)的工资要小的雇员信息
select * from emp where sal
select * from emp where sal < (select min(min(sal)) from emp group by deptno);
95.查找出20部门中没有奖金的雇员信息
select * from emp where (sal, nvl(comm, -1)) in (select sal, nvl(comm, -1) from emp where deptno = 20);
select * from emp where deptno = 20 and comm is null;
96. union操作符返回两个查询选定的所有不重复的行
select deptno from emp union select deptno from dept;
97. union all操作符合并两个查询选定的所有行,包括重复的行
select deptno from emp union all select deptno from dept;
98. intersect操作符只返回两个查询都有的行
select deptno from emp intersect select deptno from dept;
99. minus操作符只返回由第一个查询选定但是没有被第二个查询选定的行,也就是在第一个查询结果中排除在第二个查询结果中出现的行
select deptno from dept minus select deptno from emp;
��O������dL
❸ SQL语句:有个字段brand_code,我如何查询不是一数字开头的brand_code的所有信息
select brand_code from 表名 where ascii(brand_code)>57 or ascii(brand_code)<48
给分吧
ascii 是个函数,返回的是字符串的最左端的字符的ascii码
既然你在线 给分 给分
❹ sql语句 查询 非数字字符
把数字全部转化成ascii码 然后来判断。下面是个例子。
这题给5分太少了点,呵呵。
DECLARE @position int, @string char(15),@t_num varchar(50)
DECLARE cursor_r cursor for select t_num from tab2
open cursor_r
fetch next from cursor_r into @string
begin
while @@fetch_status =0
begin
--print @string
SET @position = 1
WHILE @position <= len(@string)
BEGIN
set @t_num =ASCII(SUBSTRING(@string, @position, 1))
if @t_num<48 or @t_num>57
begin
insert into tab3 (t_num) values(@string)
break
end
SET @position = @position + 1
END
fetch next from cursor_r into @string
end
close cursor_r
deallocate cursor_r
end
这题给5分太少了点,呵呵。正好我以前写过,就无私奉上了。
❺ MySQL SQL的基础应用
SQL 基础应用及information_schema
1.SQL(结构化查询语句)介绍
SQL标准:SQL 92 SQL99
5.7版本后启用SQL_Mode 严格模式
2.SQL作用
SQL 用来管理和操作MySQL内部的对象
SQL对象:
库:库名,库属性
表:表名,表属性,列名,记录,数据类型,列属性和约束
3.SQL语句的类型
DDL:数据定义语言 data definition language
DCL:数据控制语言 data control language
DML:数据操作语言 data manipulation language
DQL:数据查询语言 data query language
4.数据类型
4.1 作用:
控制数据的规范性,让数据有具体含义,在列上进行控制
4.2.种类
4.2.1 字符串
char(32)
定长长度为32的字符串。存储数据时,一次性提供32字符长度的存储空间,存不满,用空格填充。
varchar(32):
可变长度的字符串类型。存数据时,首先进行字符串长度判断,按需分配存储空间
会单独占用一个字节来记录此次的字符长度
超过255之后,需要两个字节长度记录字符长度。
面试题:
1. char 和varchar的区别?
(1) 255 65535
(2) 定长(固定存储空间) 变长(按需)
2. char和varchar 如何选择?
(1) char类型,固定长度的字符串行,比如手机号,身份证号,银行卡号,性别等
(2) varchar类型,不确定长度的字符串,可以使用。
3. enum 枚举类型
enum('bj','sh','sz','cq','hb',......)
数据行较多时,会影响到索引的应用
注意:数字类禁止使用enum类型
4.2.2 数字
1. tinyint
2. int
4.2.3 时间
1. timestamp
2. datetime
4.2.4 二进制
5. 表属性
存储引擎 :engine = InnoDB
字符集 :charset = utf8mb4
utf8 中文 三个字节长度
utf8mb4 中文 四个字节长度 才是真正的utf8
支持emoji字符
排序规则(校对规则) collation
针对英文字符串大小写问题
6. 列的属性和约束
6.1 主键: primary key (PK)
说明:
唯一
非空
数字列,整数列,无关列,自增的.
聚集索引列?
是一种约束,也是一种索引类型,在一张表中只能有一个主键。
6.2 非空: Not NULL
说明:
我们建议,对于普通列来讲,尽量设置not null
默认值 default : 数字列的默认值使用0 ,字符串类型,设置为一个nil null
6.3 唯一:unique
不能重复
6.4 自增 auto_increment
针对数字列,自动生成顺序值
6.5 无符号 unsigned
针对数字列
6.6 注释 comment
7. SQL语句应用
7.1 DDL:数据定义语言
7.1.1 库
(1)建库
mysql> create database oldguo charset utf8mb4;
mysql> show databases;
mysql> show create database oldguo;
(2)改库
mysql> alter database oldguo1 charset utf8mb4;
(3)删库
mysql> drop database oldguo1;
7.1.2 表
(0)建表建库规范:
1、库名和表名是小写字母
为啥?
开发和生产平台可能会出现问题。
2、不能以数字开头
3、不支持- 支持_
4、内部函数名不能使用
5、名字和业务功能有关(his,jf,yz,oss,erp,crm...)
(1)建表
create table oldguo (
ID int not null primary key AUTO_INCREMENT comment '学号',
name varchar(255) not null comment '姓名',
age tinyint unsigned not null default 0 comment '年龄',
gender enum('m','f','n') NOT null default 'n' comment '性别'
)charset=utf8mb4 engine=innodb;
(2)改表
1. 改表结构
-- 例子:
-- 在上表中添加一个手机号列15801332370.(重点*****)
-- alter table oldguo add telnum char(11) not null unique comment '手机号';
-- 练习:
-- 添加一个状态列
ALTER TABLE oldguo ADD state TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态列';
-- 查看列的信息
DESC oldguo;
-- 删除state列(不代表生产操作)
ALTER TABLE oldguo DROP state;
-- online-DDL : pt-osc (自己研究下***)
-- 在name后添加 qq 列 varchar(255)
ALTER TABLE oldguo ADD qq VARCHAR(255) NOT NULL UNIQUE COMMENT 'qq' AFTER NAME;
-- 练习 在name 之前添加wechat列
ALTER TABLE oldguo ADD wechat VARCHAR(255) NOT NULL UNIQUE COMMENT '微信' AFTER ID;
-- 在首列上添加 学号列:sid(linux58_00001)
ALTER TABLE oldguo ADD sid VARCHAR(255) NOT NULL UNIQUE COMMENT '学生号' FIRST;
-- 修改name数据类型的属性
ALTER TABLE oldguo MODIFY NAME VARCHAR(128) NOT NULL ;
DESC oldguo;
-- 将gender 改为 gg 数据类型改为 CHAR 类型
ALTER TABLE oldguo CHANGE gender gg CHAR(1) NOT NULL DEFAULT 'n' ;
DESC oldguo;
7.2 DML 数据操作语言
7.2.1 INSERT
--- 最简单的方法插入数据
DESC oldguo;
INSERT INTO oldguo VALUES(1,'oldguo','22654481',18);
--- 最规范的方法插入数据(重点记忆)
INSERT INTO oldguo(NAME,qq,age) VALUES ('oldboy','74110',49);
--- 查看表数据(不代表生产操作)
SELECT * FROM oldguo;
7.2.2 UPDATE (注意谨慎操作!!!!)
UPDATE oldguo SET qq='123456' WHERE id=5 ;
7.2.3 DELETE (注意谨慎操作!!!!)
DELETE FROM oldguo WHERE id=5;
7.2.4 生产需求:将一个大表全部数据清空
DELETE FROM oldguo;
TRUNCATE TABLE oldguo;
DELETE 和 TRUNCATE 区别
1. DELETE 逻辑逐行删除,不会降低自增长的起始值。
效率很低,碎片较多,会影响到性能
2. TRUNCATE ,属于物理删除,将表段中的区进行清空,不会产生碎片。性能较高。
7.2.5 生产需求:使用update替代delete,进行伪删除
1. 添加状态列state (0代表存在,1代表删除)
ALTER TABLE oldguo ADD state TINYINT NOT NULL DEFAULT 0 ;
2. 使用update模拟delete
DELETE FROM oldguo WHERE id=6;
替换为
UPDATE oldguo SET state=1 WHERE id=6;
SELECT * FROM oldguo ;
3. 业务语句修改
SELECT * FROM oldguo ;
改为
SELECT * FROM oldguo WHERE state=0;
❻ pg数据库sql如何过滤掉以9开头的数字
select *
from table
where column not like '9%'
❼ SQL里如何查询一个字段里不是数字类型的值出来
select * from 表 where isnumeric(字段) = 1
isnumeric(字段),如果为数字,则返回1,如果不为数字,则返回0~~~
❽ SQL中条件C002不等于数字69开头的语句怎么写
用like或left或substr之类的效率都很差,如果确定都是数字组成,可以使用
C002>='70' or C002<'69'
❾ sql查询出某一字段不以0和A开头的数据
sql可以使用not like 查询字段不以某个特殊字段开头的所有记录
LIKE 操作符用于在 WHERE 子句中搜索列中的指定模式。
select * from table where value not like '0%' and value not like 'A%';
❿ SQL 怎样把号码中的"00"替换成"99"
update 表名 set 字段名=replace(字段名,'00','99')
这样被指定字段里的所有00都会被换成99,如果有其他条件,请在后面加where进行限制.