标签:随机数 out outer nbsp span 学生 输出 情况下 判断
为了操作方便, 先构造以下数据
create table `student` ( `id` int unsigned primary key auto_increment, `name` char(32) not null unique, `sex` enum(‘男‘, ‘女‘) not null, `city` char(32) not null, `description` text, `birthday` date not null default ‘1995-1-1‘, `money` float(7, 2) default 0, `only_child` boolean ) charset=utf8; insert into `student` (`name`, `sex`, `city`, `description`, `birthday`, `money`, `only_child`) values (‘郭德纲‘, ‘男‘, ‘北京‘, ‘班长‘, ‘1997/10/1‘, rand() * 100, True), (‘陈乔恩‘, ‘女‘, ‘上海‘, NULL, ‘1995/3/2‘, rand() * 100, True), (‘赵丽颖‘, ‘女‘, ‘北京‘, ‘班花, 不骄傲‘, ‘1995/4/4‘, rand() * 100, False), (‘王宝强‘, ‘男‘, ‘重庆‘, ‘超爱吃火锅‘, ‘1998/10/5‘, rand() * 100, False), (‘赵雅芝‘, ‘女‘, ‘重庆‘, ‘全宇宙三好学生‘, ‘1996/7/9‘, rand() * 100, True), (‘张学友‘, ‘男‘, ‘上海‘, ‘奥林匹克总冠军!‘, ‘1993/5/2‘, rand() * 100, False), (‘陈意涵‘, ‘女‘, ‘上海‘, NULL, ‘1994/8/30‘, rand() * 100, True), (‘赵本山‘, ‘男‘, ‘南京‘, ‘副班长‘, ‘1995/6/1‘, rand() * 100, True), (‘张柏芝‘, ‘女‘, ‘上海‘, NULL, ‘1997/2/28‘, rand() * 100, False), (‘吴亦凡‘, ‘男‘, ‘南京‘, ‘大碗宽面要不要?‘, ‘1995/6/1‘, rand() * 100, True), (‘鹿晗‘, ‘男‘, ‘北京‘, NULL, ‘1993/5/28‘, rand() * 100, True), (‘关晓彤‘, ‘女‘, ‘北京‘, NULL, ‘1995/7/12‘, rand() * 100, True), (‘周杰伦‘, ‘男‘, ‘台北‘, ‘小伙人才啊‘, ‘1998/3/28‘, rand() * 100, False), (‘马云‘, ‘男‘, ‘南京‘, ‘一个字:贼有钱‘, ‘1990/4/1‘, rand() * 100, False), (‘马化腾‘, ‘男‘, ‘上海‘, ‘马云死对头‘, ‘1990/11/28‘, rand() * 100, False);
create table score ( `id` int unsigned primary key auto_increment, `math` float not null default 0, `english` float not null default 0 ) charset=utf8; insert into score (`math`, `english`) values (49, 71), (62, 66.7), (44, 86), (77.5, 74), (41, 75), (82, 59.5), (64.5, 85), (62, 98), (44, 36), (67, 56), (81, 90), (78, 70), (83, 66), (40, 90), (90, 90);
select rand(); -- 随机数 select unix_timestamp(); -- 显示Unix时间戳 select id, name from student;
SELECT student.name, score.math FROM student, score;
select name from student where city = ‘上海‘;
select `name` from `student` where `description` is null; select `name` from `student` where `description` is not null;
select id, math from score where math between 60 and 70; select id, math from score where math not between 60 and 70; select * from score where math>=80 and english<=60; -- 直接做比较判断
select `name`, `birthday` from `student` where `birthday` > ‘1995-1-1‘; select `name`, `birthday` from `student` having `birthday` > ‘1995-1-1‘; select * from student where id>=3 and city=‘北京‘; select * from student having id>=3 and city=‘北京‘; select * from student where id>=3 having city=‘北京‘; -- 混用
但是他们也有区别。
select `name`, `birthday` from `student` where id > 2; -- 报错,having的条件查询,只能包含在前面的搜索结果里 select `name`, `birthday` from `student` having id > 2;
select name as n,birthday as b,id as i from student having i > 2; -- 报错,where只识别存在的字段 select name as n,birthday as b,id as i from student where i > 2; -- 取出每个城市中满足最小出生年份大于1995的 select city, group_concat(birthday) from student group by city having min(birthday) > ‘1995-1-1‘;
select sex, count(id) from student group by sex; -- 在group将需要的结果通过 “聚合函数” 拼接 select sex, group_concat(name) from student group by sex;
select * from student order by age; select * from student order by age desc; select city,avg(money),group_concat(name),sum(money) from student group by city having sum(money)>70 order by sum(money);
语法:
select 字段 from 表名 limit m; -- 从第 1 行到第 m 行 select 字段 from 表名 limit m, n; -- 从第 m 行开始,往下取 n 行 select 字段 from 表名 limit m offset n; -- 跳过前 n 行, 取后面的 m 行
示例:
select distinct city from student;
dual 是一个虚拟表, 仅仅为了保证 select ... from ... 语句的完整性
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
union要求:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
SELECT 字段 FROM 表1 INNER JOIN 表2 ON 表1.字段=表2.字段; -- 或: SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name=table2.column_name;
LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。
语法
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name; -- 或: SELECT column_name(s) FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name;
RIGHT JOIN 关键字从右表(table2)返回所有的行,即使左表(table1)中没有匹配。如果左表中没有匹配,则结果为 NULL。
语法
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name; -- 或: SELECT column_name(s) FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name=table2.column_name;
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
查询的语句中还有一个查询
select name from student where id in (select id from score where math > 10);
create view v_user_score as select a.id, a.name, b.math, b.english from student a inner join score b on a.id=b order by id; -- 查询 select * from v_user_score; -- 删除 drop view v_user_score;
标签:随机数 out outer nbsp span 学生 输出 情况下 判断
原文地址:https://www.cnblogs.com/zhuzhaoli/p/14225834.html