CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
insert into titles_test values ('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
UPDATE titles_test
SET emp_no =
REPLACE(emp_no,10001,100005)
WHERE id = 5;
SELECT REPLACE('aaa.mysql.com','a','w');
结果:www.mysql.com
CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
ALTER TABLE titles_test RENAME TO titles_2017
CREATE TABLE employees_test(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);
CREATE TABLE audit(
EMP_no INT NOT NULL,
create_date datetime NOT NULL);
仅针对于MySql:
ALTER TABLE audit
ADD FOREIGN KEY(emp_no)
REFERENCES employees_test(id);
存在如下的视图:
create view emp_v as select * from employees where emp_no >10005;
如何获取emp_v和employees有相同的数据?
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
emp_no | birth_date | first_name | last_name | gender | hire_date |
---|---|---|---|---|---|
10006 | 1953-04-20 | Anneke | Preusig | F | 1989-06-02 |
10007 | 1957-05-23 | Tzvetan | Zielinski | F | 1989-02-10 |
1. 交集(不适用于MySQL,MySQL中不存在交集关键字)
SELECT * FROM employees
INTERSECT
SELECT * FROM emp_v;
2. WHERE 取 emp_no 相等的记录
SELECT em.*
FROM employees em,emp_v ev
WHERE em.emp_no = ev.emp_no;
3. 既然 emp_v 就是从 employees 中取出来的,直接输出 emp_v 不就OK了
SELECT * FROM emp_v;
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AU80IMik-1571891889097)(images/1569081998068.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-USNEaucB-1571891889100)(images/1569082033854.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EHTPqibo-1571891889103)(images/1569082225332.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DK0i8suu-1571891889106)(images/1569082254275.png)]
create table emp_bonus(
emp_no int not null,
recevied datetime not null,
btype smallint not null);
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));
解析:1. 首先从emp_bonus中获取获奖的员工emp_no,
2. 然后从salaries中对应的emp_no使其salary增加10%
UPDATE salaries
SET salary = salary * 1.1
WHERE emp_no IN(
SELECT s.emp_no FROM salaries s
INNER JOIN emp_bonus e
ON s.emp_no = e.emp_no
AND s.to_date = '9999-01-01'
)
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
create table emp_bonus(
emp_no int not null,
recevied datetime not null,
btype smallint not null);
牛客网的数据库系统是SQLite
SELECT "select count(*) from " || name || ";" as cnts
FROM sqlite_master
WHERE type = 'table'
下面是针对于MySQL的(MySQL连接字符串需要使用 concant() 的函数)
SELECT CONCAT("select count(*) from ",table_name,";") as cnts
FROM (SELECT table_name from information_schema.tables where table_schema = 'test')
AS test
必须要有 AS xxx,随便指定一个别名就行,否则会报错Every derived table must have its own alias
通过FROM子句生成派生表时,AS关键字可以省略,但必须为派生表指定一个别名。”引自《数据库系统概论》3.4.5节
MySQL元数据库——information_schema
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
SELECT last_name || "'"|| first_name FROM employees
mysql:
SELECT CONCAT(last_name,"'",first_name) FROM employees
仅适用于 SQLite
先使用replace将原串中的子串","使用空串""替代
再用原串长度减去替换后的字符串的长度
最后除以子串的长度(本题可省略,子串长度大于1,则不可以省略)
SELECT (length("10,A,B")-length(replace("10,A,B",",","")))/length(",") AS cnt
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
SQLite
SELECT first_name
FROM employees
ORDER BY SUBSTR(first_name,-2);
mysql:
SELECT first_name
FROM employees
ORDER BY SUBSTRING(first_name,-2);
从字符串 s 的 start 位置截取长度为 length 的子字符串
从字符串 RUNOOB 中第2个位置开始向后截取3个字符,(第一个字符的编号是1)
SELECT SUBSTRING("RUNOOB", 2, 3); -- UNO
SUBSTRING(s, length)
length为正数,返回左数length到末尾
SELECT SUBSTRING("RUNOOB", 2); -- UNOOB
length为负数,返回右数length绝对值到末尾
SELECT SUBSTRING("RUNOOB", 2); -- OB
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
SQLite:
SELECT dept_no,GROUP_CONCAT(emp_no) employees
FROM dept_emp
GROUP BY dept_no
MySQL:
select dept_no,group_concat(emp_no SEPARATOR ',') from dept_emp group by dept_no;
将group by产生的同一个分组中的值连接起来,返回一个字符串结果。
separator 为分隔符,缺省(默认)是一个逗号","
参考文章:Group_concat介绍与例子
CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));
使用 avg()函数就OK了,注意排除最大和最小salary就好
SELECT AVG(salary) FROM salary
WHERE to_date = '9999-01-01'
AND salary NOT IN(SELECT MAX(salary) FROM salary WHERE to_date = '9999-01-01')
AND salary NOT IN(SELECT MIN(salary) FROM salary WHERE to_date = '9999-01-01')
这个在牛客网的判别系统中是通过不了的,去掉查询最大、最小salary的where语句才能通过,但这个是系统的错误。
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
SELECT *
FROM employees
LIMIT 5,5
CREATE TABLE `dept_emp` ( `emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
create table emp_bonus(
emp_no int not null,
recevied datetime not null,
btype smallint not null);
SELECT e.emp_no,de.dept_no,eb.btype,eb.recevied
FROM employees e
INNER JOIN dept_emp de ON e.emp_no = de.emp_no
LEFT JOIN emp_bonus eb ON e.emp_no = eb.emp_no
有点懵
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
SELECT * FROM employees WHERE NOT EXISTS
(SELECT emp_no FROM dept_emp WHERE emp_no = employees.emp_no)
使用 NOT IN
SELECT * FROM employees WHERE emp_no NOT IN
(SELECT emp_no FROM dept_emp)
存在如下的视图:
create view emp_v as select * from employees where emp_no >10005;
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
SELECT * FROM employees WHERE EMP_NO IN (SELECT emp_no FROM emp_v)
SELECT * FROM emp_v
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
create table emp_bonus(
emp_no int not null,
recevied datetime not null,
btype smallint not null);
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));
给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。
bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。 当前薪水表示to_date='9999-01-01'
SELECT e.emp_no,e.first_name,e.last_name,eb.btype,s.salary,
(CASE eb.btype
WHEN 1 THEN s.salary * 0.1
WHEN 2 THEN s.salary * 0.2
ELSE s.salary * 0.3
END) bonus
FROM employees e,emp_bonus eb,salaries s
ON e.emp_no = s.emp_no
AND e.emp_no = eb.emp_no
AND s.to_date = '9999-01-01'
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
select e1.first_name
from employees e1
where (select count(*)
from employees e2
where e1.first_name > e2.first_name )%2 ==0;
有点懵