當前位置:首頁 » 編程語言 » 不是數字99開頭的sql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

不是數字99開頭的sql

發布時間: 2022-11-26 07:41:19

❶ 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進行限制.