MySql 查询语句

SELECT DATABASE(); // 查询正在使用的数据库
SHOW TABLES; // 查询数据库里的表
DESC db; // 查询表的结构
基本结构
SELECT [DISTINCT] column1, column2, ..., columnN
FROM table_name
WHERE condition
GROUP BY column1, column2, ..., columnN
HAVING condition
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
LIMIT number;

方言

不同数据库管理系统(DBMS)实现SQL(结构化查询语言)时,可能在语法、功能和行为上存在的差异

DUAL(虚拟表)
SELECT 'Hello, World!' FROM DUAL;
  • 执行简单的计算:
SELECT 2 + 2 FROM DUAL;# 4
SELECT 语句的基本语法
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column_name;

SELECT * FROM Employees;//查询所有
SELECT FirstName, LastName FROM Employees;//查询指定列
SELECT * FROM Employees WHERE Age > 30;//条件判断
SELECT * FROM Employees ORDER BY LastName ASC;//排序结果
  • 起别名:
select employee_id e_id from list;//e_id即为别名
select employee_id AS e_id from list;//可以加上as关键字,也可省略
  • DISTINCT去重:
SELECT DISTINCT 列名1, 列名2,FROM 表名;
空值
  • 空值不是0
  • NULL 参与算术运算时,结果一般为 NULL
SELECT 5 + NULL;   -- NULL
SELECT NULL * 10;  --  NULL
NULL 与逻辑运算符运算
  • AND 运算:
    • TRUE AND NULLNULL
    • FALSE AND NULLFALSE(因为 FALSE 已经决定结果)
    • NULL AND NULLNULL
  • OR 运算:
    • TRUE OR NULLTRUE(因为 TRUE 已经决定结果)
    • FALSE OR NULLNULL
    • NULL OR NULLNULL
常数查询:返回一个固定的值
SELECT 1;
SELECT 'Hello, World!';
SELECT 100 AS constant_value;//给常数加别名
SELECT 'SQL常数查询' AS message;//字符串常数
DESCRIBE course;#查看表结构

DESC course;#查看表结构

SHOW COLUMNS FROM course;# 类似于查询表结构

SHOW CREATE TABLE course;# 查询建表语句

内连接:

1. 显式内连接

使用 INNER JOIN 关键字进行连接.

SELECT1.,2.FROM1
INNER JOIN2 ON1.连接列 =2.连接列;

示例:employees 表和 departments 表通过 department_id 连接

SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
2. 隐式内连接

使用 WHERE 子句来指定连接条件.

SELECT1.,2.FROM1,2
WHERE1.连接列 =2.连接列;

示例:

SELECT employees.name, departments.department_name
FROM employees, departments
WHERE employees.department_id = departments.id;

外连接:

1. 左外连接(LEFT JOIN)

返回左表中的所有记录,即使在右表中没有匹配的记录。如果右表列中没有匹配,返回 NULL

SELECT1.,2.FROM1
LEFT JOIN2 ON1.连接列 =2.连接列;

示例:

SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;
2. 右外连接(RIGHT JOIN)

和左外连接相反.

SELECT1.,2.FROM1
RIGHT JOIN2 ON1.连接列 =2.连接列;

示例:

SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
3. 全外连接(FULL JOIN)

全外连接会返回左表和右表中所有的记录。如果左表或右表中没有匹配的记录,相应的列返回 NULL

SELECT1.,2.FROM1
FULL JOIN2 ON1.连接列 =2.连接列;

示例:

SELECT employees.name, departments.department_name
FROM employees
FULL JOIN departments ON employees.department_id = departments.id;
-- 准备数据  
create table dept(  
    id   int auto_increment comment 'ID' primary key,  
    name varchar(50) not null comment '部门名称'  
)comment '部门表';  
  
create table emp(  
    id  int auto_increment comment 'ID' primary key,  
    name varchar(50) not null comment '姓名',  
    age  int comment '年龄',  
    job varchar(20) comment '职位',  
    salary int comment '薪资',  
    entrydate date comment '入职时间',  
    managerid int comment '直属领导ID',  
    dept_id int comment '部门ID'  
)comment '员工表';  
  
-- 添加外键  
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);  
  
INSERT INTO dept (id, name) VALUES (1, '研发部'), (2, '市场部'),(3, '财务部'), (4, '销售部'), (5, '总经办'), (6, '人事部');  
INSERT INTO emp (id, name, age, job,salary, entrydate, managerid, dept_id) VALUES  
            (1, '金庸', 66, '总裁',20000, '2000-01-01', null,5),  
  
            (2, '张无忌', 20, '项目经理',12500, '2005-12-05', 1,1),  
            (3, '杨逍', 33, '开发', 8400,'2000-11-03', 2,1),  
            (4, '韦一笑', 48, '开发',11000, '2002-02-05', 2,1),  
            (5, '常遇春', 43, '开发',10500, '2004-09-07', 3,1),  
            (6, '小昭', 19, '程序员鼓励师',6600, '2004-10-12', 2,1),  
  
            (7, '灭绝', 60, '财务总监',8500, '2002-09-12', 1,3),  
            (8, '周芷若', 19, '会计',48000, '2006-06-02', 7,3),  
            (9, '丁敏君', 23, '出纳',5250, '2009-05-13', 7,3),  
  
            (10, '赵敏', 20, '市场部总监',12500, '2004-10-12', 1,2),  
            (11, '鹿杖客', 56, '职员',3750, '2006-10-03', 10,2),  
            (12, '鹤笔翁', 19, '职员',3750, '2007-05-09', 10,2),  
            (13, '方东白', 19, '职员',5500, '2009-02-12', 10,2),  
  
            (14, '张三丰', 88, '销售总监',14000, '2004-10-12', 1,4),  
            (15, '俞莲舟', 38, '销售',4600, '2004-10-12', 14,4),  
            (16, '宋远桥', 40, '销售',4600, '2004-10-12', 14,4),  
            (17, '陈友谅', 42, null,2000, '2011-10-12', 1,null);  
  
  
-- 多表查询 -- 笛卡尔积  
# 几种连接查询方式  
select emp.name,dept.name from emp,dept where emp.dept_id = dept.id; # 隐式内连接  
select * from emp inner join dept on emp.dept_id = dept.id; # 显式内连接,inner可以省略  
select dept.*,emp.* from emp left outer join dept on emp.dept_id = dept.id; # 左外连接,outer可以省略 完全包含左表的信息  
# 即可以查询emp表(左表)中所有信息  
  
select dept.*,emp.* from emp right outer join dept on emp.dept_id = dept.id; # 右外连接,outer可以省略  
# 即可以查询dept表(右表)中所有信息  
  
  
select *from dept;  
select *from emp;  
  
drop table emp,dept;  
  
  
  
select a.name,b.name from emp a,emp b where a.managerid=b.id; #自链接,把一张表看成两张表,必须起别名  
select a.name,b.name from emp a left join emp b on a.managerid=b.id;  
  
  
# 联合查询  
select *from emp where salary>4800  
union all  
select *from emp where age>44;# 将两个select语句查询的结果和在一起,注意:查询的列数需要相同  
  
  
#子查询  
  
select id from dept where name='销售部';  
select *from emp where dept_id=4;  
  
#将多条语句合并为一条  
select *from emp where dept_id=(select id from dept where name='销售部');# 标量子查询  
  
select *from emp where dept_id in(select id from dept where name='销售部' or name='市场部');# 列子查询,返回一列或者多行  
  
select * from emp where salary >all(select emp.salary from emp where dept_id = (select id from dept where name ='财务部'));#all用于判断是否满足所有的要求并返回  
  
  
select emp.salary from emp where salary >any  
(select emp.salary from emp where dept_id=(select id from dept where name ='研发部'));# any满足一个条件即可  
  
  
select *from emp where(salary,managerid)=(select salary,managerid where name='张无忌'); #行子查询,返回一行或多列  
  
  
select *from emp where (job,salary) in(select job,salary from emp where name='鹿杖客' or name='宋远桥');#表子查询,返回多行多列  
  
  
select e.*,d.* from (select *from emp where entrydate>'2006-1-1')  e left join dept d on e.dept_id=d.id;#子查询作为一张临时表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值