MySQl数据库必会sql语句加强版
这篇承接上一篇《mysql必会sql语句》:http://blog.csdn.net/qq_32059827/article/details/51763950
这一篇属于加强版,问题和sql语句如下。
创建users表,设置id,name,gender,sal字段,其中id为主键
drop table if exists users;
create table if not exists users(
id int(5) primary key auto_increment,
name varchar(10) unique not null,
gender varchar(1) not null,
sal int(5) not null
);
insert into users(name,gender,sal) values('AA','男',1000);
insert into users(name,gender,sal) values('BB','女',1200);
--------------------------------------------------------------------------------------
一对一:AA的身份号是多少
drop table if exists users;
create table if not exists users(
id int(5) primary key auto_increment,
name varchar(10) unique not null,
gender varchar(1) not null,
sal int(5) not null
);
insert into users(name,gender,sal) values('AA','男',1000);
insert into users(name,gender,sal) values('BB','女',1200); drop table if exists cards;
create table if not exists cards(
id int(5) primary key auto_increment,
num int(3) not null unique,
loc varchar(10) not null,
uid int(5) not null unique,
constraint uid_fk foreign key(uid) references users(id)
);
insert into cards(num,loc,uid) values(111,'北京',1);
insert into cards(num,loc,uid) values(222,'上海',2);
【注:inner join表示内连接】
select u.name "姓名",c.num "身份证号"
from users u inner join cards c
on u.id = c.uid
where u.name = 'AA';
--
select u.name "姓名",c.num "身份证号"
from users u inner join cards c
on u.id = c.uid
where name = 'AA'; ---------------------------------------------
一对多:查询"开发部"有哪些员工
创建groups表
drop table if exists groups;
create table if not exists groups(
id int(5) primary key auto_increment,
name varchar(10) not null
);
insert into groups(name) values('开发部');
insert into groups(name) values('销售部'); 创建emps表
drop table if exists emps;
create table if not exists emps(
id int(5) primary key auto_increment,
name varchar(10) not null,
gid int(5) not null,
constraint gid_fk foreign key(gid) references groups(id)
);
insert into emps(name,gid) values('哈哈',1);
insert into emps(name,gid) values('呵呵',1);
insert into emps(name,gid) values('嘻嘻',2);
insert into emps(name,gid) values('笨笨',2); 查询"开发部"有哪些员工
select g.name "部门",e.name "员工"
from groups g inner join emps e
on g.id = e.gid
where g.name = '开发部';
--
select g.name "部门",e.name "员工"
from groups g inner join emps e
on g.id = e.gid
where g.name = '开发部';
------------------------------------------------------
多对多:查询"赵"教过哪些学生
创建students表
drop table if exists students;
create table if not exists students(
id int(5) primary key auto_increment,
name varchar(10) not null
);
insert into students(name) values('哈哈');
insert into students(name) values('嘻嘻'); 创建teachers表
drop table if exists teachers;
create table if not exists teachers(
id int(5) primary key auto_increment,
name varchar(10) not null
);
insert into teachers(name) values('赵');
insert into teachers(name) values('刘'); 创建middles表 primary key(sid,tid) 表示联合主键,这两个字段的整体要唯一
drop table if exists middles;
create table if not exists middles(
sid int(5),
constraint sid_fk foreign key(sid) references students(id),
tid int(5),
constraint tid_fk foreign key(tid) references teachers(id),
primary key(sid,tid)
);
insert into middles(sid,tid) values(1,1);
insert into middles(sid,tid) values(1,2);
insert into middles(sid,tid) values(2,1);
insert into middles(sid,tid) values(2,2); 查询"赵"教过哪些学生
select t.name "老师",s.name "学生"
from students s inner join middles m inner join teachers t
on (s.id=m.sid) and (m.tid=t.id)
where t.name = '赵';
--
select t.name "老师",s.name "学生"
from students s inner join middles m inner join teachers t
on (s.id=m.sid) and (t.id=m.tid)
where t.name = "赵"; --------------------------------------------------------------------------------------------------------
将5000元(含)以上的员工标识为"高薪",否则标识为"起薪" 将薪水为NULL的员工标识为"无薪" 将5000元(含)以上的员工标识为"高薪",否则标识为"起薪" 将7000元的员工标识为"高薪",6000元的员工标识为"中薪",5000元则标识为"起薪",否则标识为"试用薪"
---------------------------------------------------------------------------------------------------------
内连接(等值连接):查询客户姓名,订单编号,订单价格
【注:customers c inner join orders o使用了别名,以后o就代表orders】
select c.name "客户姓名",o.isbn "订单编号",o.price "订单价格"
from customers c inner join orders o
on c.id = o.customers_id;
--
select c.name "客户姓名",o.isbn "订单编号",o.price "订单价格"
from customers c inner join orsers o
on c.id = o.customers_id;
on+两张表连接的条件.一张表的主键,一张表的外键
内连接:只能查询出二张表中根据连接条件都存在的记录,有点类似于数学中交集
----------------------------------------------------
外连接:按客户分组,查询每个客户的姓名和订单数
外连接:既可以根据连接条件查询出二张表中都存在的记录,也能根据一方,强行将另一方就算不满兄条件的记录也能查询出来
外连接可以细分为:
<左外连接 : 以左侧为参照,left outer join表示
select c.name,count(o.isbn)
from customers c left outer join orders o
on c.id = o.customers_id
group by c.name;
--
>右外连接 : 以右侧为参照,right outer join表示
select c.name,count(o.isbn)
from orders o right outer join customers c
on c.id = o.customers_id
group by c.name; left outer join表示左边的内容都会显现出来,例如customers c left out join 表示会把customers中的某列所有内容都找出来 ------------------------------------------------------
自连接:求出AA的老板是EE。把自己想象成两张表。左右各一张
select users.ename,bosss.ename
from emps users inner join emps bosss
on users.mgr = bosss.empno; select users.ename,bosss.ename
from emps users left outer join emps bosss
on users.mgr = bosss.empno; -----------------------------------------------------------------------------------------------
演示MySQL中的函数(查询手册) 日期时间函数:
select addtime('2016-8-7 23:23:23','1:1:1'); 时间相加
select current_date();
select current_time();
select now();
select year( now() );
select month( now() );
select day( now() );
select datediff('2016-12-31',now()); 字符串函数:
select charset('哈哈');
select concat('你好','哈哈','吗');
select instr('www.baidu.com','baidu');
select substring('www.baidu.com',5,3); 数学函数:
select bin(10);
select floor(3.14);//比3.14小的最大整数---正3
select floor(-3.14);//比-3.14小的最大整数---负4
select ceiling(3.14);//比3.14大的最小整数---正4
select ceiling(-3.14);//比-3.14大的最小整数---负3,一定是整数值
select format(3.1415926,3);保留小数点后3位,四舍五入
select mod(10,3);//取余数
select rand();// 加密函数:
select md5('123456');
返回32位16进制数 e10adc3949ba59abbe56e057f20f883e 演示MySQL中流程控制语句
use json;
drop table if exists users;
create table if not exists users(
id int(5) primary key auto_increment,
name varchar(10) not null unique,
sal int(5)
);
insert into users(name,sal) values('哈哈',3000);
insert into users(name,sal) values('呵呵',4000);
insert into users(name,sal) values('嘻嘻',5000);
insert into users(name,sal) values('笨笨',6000);
insert into users(name,sal) values('明明',7000);
insert into users(name,sal) values('丝丝',8000);
insert into users(name,sal) values('君君',9000);
insert into users(name,sal) values('赵赵',10000);
insert into users(name,sal) values('无名',NULL); 将5000元(含)以上的员工标识为"高薪",否则标识为"起薪"
select name "姓名",sal "薪水",
if(sal>=5000,"高薪","起薪") "描述"
from users; 将薪水为NULL的员工标识为"无薪"
select name "姓名",ifnull(sal,"无薪") "薪水"
from users; 将5000元(含)以上的员工标识为"高薪",否则标识为"起薪"
select name "姓名",sal "薪水",
case when sal>=5000 then "高薪"
else "起薪" end "描述"
from users; 将7000元的员工标识为"高薪",6000元的员工标识为"中薪",5000元则标识为"起薪",否则标识为"试用薪"
select name "姓名",sal "薪水",
case sal
when 3000 then "低薪"
when 4000 then "起薪"
when 5000 then "试用薪"
when 6000 then "中薪"
when 7000 then "较好薪"
when 8000 then "不错薪"
when 9000 then "高薪"
else "重薪"
end "描述"
from users;
MySQl数据库必会sql语句加强版的更多相关文章
- MySQL数据库-表操作-SQL语句(二)
1. MySQL多表查询 1.1 外键约束 为了消除多张表查询出现的笛卡尔积的现象,MySQL在建表并进行多表之间的关键查询可以使用外键关联查询. 外键:从表1(sub)的某列引用(ref)另外一个表 ...
- MySQL数据库-表操作-SQL语句(一)
1. 数据库操作与存储引擎 1.1 数据库和数据库对象 数据库对象:存储,管理和使用数据的不同结构形式,如:表.视图.存储过程.函数.触发器.事件等. 数据库:存储数据库对象的容器. 数据库分两种 ...
- MySQL数据库基本操作以及SQL语句
连接mysql的语法 mysql -u用户名 -p密码 [-h主机名] [-P端口号] 在一个mysql服务器中, 可以有多个mysql数据库(本质是一个文件夹) 在一个mysql数据库中, 可以有多 ...
- mysql数据库之基础SQL语句/语法
SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库 Create DATABAS ...
- MySQL数据库(一)—— 数据库介绍、MySQL安装、基础SQL语句
数据库介绍.MySQL安装.基础SQL语句 一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 ...
- 通过MyEclipse工具直接操作数据库,执行sql语句,方便快捷
原文:通过MyEclipse工具直接操作数据库,执行sql语句,方便快捷 通过MyEclipse操作数据库,执行sql语句使我们不用切换多个工具,直接工作,方便快捷.效果如下: 步骤1:通过MyEcl ...
- 如何用VS EF连接 Mysql,以及执行SQL语句 和存储过程?
VS2013, MySQL5.7.18 , MySQL5.7.14 执行SQL语句: ztp_user z = new ztp_user(); object[] obj = new object[] ...
- MySQL数据库(增删改查语句)
MySQL数据库(增删改查语句)一.登录数据库:----> mysql -uroot -proot;(对应用户名和密码)二.SQL语句: 数据定义语言DDL 用来定义数据库.表.列,关 ...
- 【转】MySQL用户管理及SQL语句详解
[转]MySQL用户管理及SQL语句详解 1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysq ...
随机推荐
- [APIO2008]
A.免费道路roads 题意:给定n个点m条边的图,边有黑白颜色,求是否有一个生成树满足恰好有K条黑边,并输出方案. 题解:先加白边,求出必须加的黑边,然后加黑边到K条,剩下的用白边填充. 顺便说说, ...
- Codeforces Round #407 (Div. 2)
来自FallDream的博客,未经允许,请勿转载,谢谢. ------------------------------------------------------ A.Anastasia and ...
- 机器学习基础—集成学习Bagging 和 Boosting
集成学习 就是不断的通过数据子集形成新的规则,然后将这些规则合并.bagging和boosting都属于集成学习.集成学习的核心思想是通过训练形成多个分类器,然后将这些分类器进行组合. 所以归结为(1 ...
- 求n的阶乘
import java.util.Scanner; public class J { public static void main(String args[]) { //注释:int n=6 ...
- 如何使用 TeamViewer 配置QuickConnect按钮?
QuickConnect作为TeamViewer中一个比较重要的部分,得到了很多用户的认可.那么在实际运用中,怎么才能设置网页或单个程序的QuickConnect呢?所以小编以此问题为例,教大家如何配 ...
- Linux sort -n 与 -g 排序对比
公司业务需要,天天用awk统计数值然后排序,出问题了,如下: count.sh 是一个统计脚本,把awk输出的值按占比.条数.类型 在重新输出 awk -F\| '{print $16}' *MQTT ...
- 使用foreach需要判空。
今天写代码的时候,需要遍历一个作为参数传递进来的容器, 当时顺手就加上了判空条件: if(null==list)return; 后来就像,不知道遍历(foreach)有没有帮我做这个工作: 下面看实验 ...
- 关于一些基础的Java问题的解答(四)
16. Java面向对象的三个特征与含义 java中的面向对象的三大基本特征分别是:封装.继承.多态: 封装:把过程和数据包围起来,对数据的访问只能通过已定义的界面,主要是方便类的修改 继承:对象的一 ...
- 用一个div模拟textarea的实现
<textarea> 标签定义一个多行的文本输入控件.但是它不能像div一样随着内容增加而自动增加,一言不合就出现滚动条,有是有为了更好的交互,可能需要使用div来模拟textarea的实 ...
- rw 模板设置
在编译的时候只有写出rw之后使用alt+/就可以将模板代码全部展现出来. rw读写模板代码: InputStream in = null; OutputStream out = null; try { ...