User-Defined Variables
mysql> SET @w := SELECT COUNT(*) FROM course WHERE cteacher='程军';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
the right syntax to use near 'SELECT COUNT(*) FROM course WHERE cteacher='程军'' at line 1
mysql> SELECT @w;
+------+
| @w |
+------+
| NULL |
+------+
1 row in set (0.00 sec) mysql> SET @w := (SELECT COUNT(*) FROM course WHERE cteacher='程军');
Query OK, 0 rows affected (0.00 sec) mysql> SELECT @w;
+------+
| @w |
+------+
| 2 |
+------+
1 row in set (0.00 sec) mysql>
SHOW TABLES;
DROP TABLE IF EXISTS student;
SHOW TABLES;
CREATE TABLE IF NOT EXISTS student
(
sno int primary key,
sname VARCHAR(20) CHARACTER SET gbk,
sage TINYINT,
shair ENUM('黑','白') CHARACTER SET gbk
);
INSERT INTO student VALUES('','李强',23,'黑');
INSERT INTO student VALUES('','刘丽',22,'白');
INSERT INTO student VALUES('','张友',22,'黑');
SELECT * FROM student; SHOW TABLES;
DROP TABLE IF EXISTS course;
SHOW TABLES;
CREATE TABLE IF NOT EXISTS course
(
cno VARCHAR(10) primary key,
cname VARCHAR(20) CHARACTER SET gbk,
cteacher VARCHAR(20) CHARACTER SET gbk
);
INSERT INTO course VALUES('k1','c语言','王华');
INSERT INTO course VALUES('k5','数据库原理','程军');
INSERT INTO course VALUES('k8','编译原理','程军');
SELECT * FROM course; SHOW TABLES;
DROP TABLE IF EXISTS sc;
SHOW TABLES;
CREATE TABLE IF NOT EXISTS sc
(
fk_sno INT NOT NULL,
fk_cno VARCHAR(10) NOT NULL,
score INT NOT NULL,
PRIMARY KEY(fk_sno,fk_cno),
FOREIGN KEY(fk_sno) REFERENCES student(sno),
FOREIGN KEY(fk_cno) REFERENCES course(cno)
);
INSERT INTO sc VALUES('','K1',83);
INSERT INTO sc VALUES('','K1',85);
INSERT INTO sc VALUES('','K1',92);
INSERT INTO sc VALUES('','K5',90);
INSERT INTO sc VALUES('','K5',84);
INSERT INTO sc VALUES('','K8',80);
SELECT * FROM sc;
1-检索至少选修"程军"老师所授全部课程的学生姓名(SNAME)
检索选修了“程军”老师所授全部课程的学生姓名
0-检索选修了“程军”老师所授课程的学生
SELECT * FROM sc RIGHT JOIN course ON sc.fk_cno=course.cno;
SELECT * FROM (SELECT * FROM sc RIGHT JOIN course ON sc.fk_cno=course.cno) w RIGHT JOIN student ON w.fk_sno=student.sno;
SELECT * FROM (SELECT * FROM sc RIGHT JOIN course ON sc.fk_cno=course.cno) w RIGHT JOIN student ON w.fk_sno=student.sno WHERE cteacher='程军';
集合子集,逐步精确,缩小范围 1-是学生-2-选程老师的课-3-选了程老师所有的课
/*
假定
“ 某学生选程老师的课程门数等于程老师所开门数”等价于“该学生至少选了程老师的所有的课程”。
*/
SELECT sname FROM student
WHERE
(
SELECT COUNT(*) FROM
(
SELECT * FROM (SELECT * FROM sc RIGHT JOIN course ON sc.fk_cno=course.cno) w RIGHT JOIN student ON w.fk_sno=student.sno
WHERE cteacher = '程军'
) wb
WHERE wb.sname = student.sname
)
=
(SELECT COUNT(*) FROM course WHERE cteacher='程军');
/* 0-澄清问题:找出至少选了程老师全部课程的学生:学生-没有选程老师的课+学生选了程老师的部分的课+学生选了程老师全部的课
等价转化:找出选了程老师部分的课或者没有选程老师的课的学生
数据关系:学生和老师没有数据关系,是两个完全独立的数据对象,分数是桥梁
等价转化:
选了部分程老师的课 ,NOT EXISTS (SELECT * FROM sc WHERE sc.sno = student AND sc.cno=course.cno)->FALSE (内层SELECT不空,TRUE,退出)
没有选程老师的课 teacher='程军'->FALSE ,后边的不用看了(中层SELECT空,FALSE,退出)
外中内-三层SELECT,外层返回的是最终的结果
最外层选得的最终量result,学生姓名,它的措施是将各个学生名称带入,逐个检测
1-程序表达: */
SELECT sname FROM student
WHERE NOT EXISTS
(
SELECT * FROM course
WHERE cteacher='程军' AND NOT EXISTS
(
SELECT * FROM sc
WHERE sc.fk_sno = student.sno AND sc.fk_cno = course.cno
)
) ;
2- 检索选修全部课程的学生姓名(SNAME)。
SELECT sname FROM student
WHERE
(
SELECT COUNT(*) FROM
(
SELECT * FROM (SELECT * FROM sc RIGHT JOIN course ON sc.fk_cno=course.cno) w RIGHT JOIN student ON w.fk_sno=student.sno
) wb
WHERE wb.sname = student.sname
)
=
(SELECT COUNT(*) FROM course ); SELECT sname FROM student
WHERE NOT EXISTS
(
SELECT * FROM course
WHERE NOT EXISTS
(
SELECT * FROM sc
WHERE sc.fk_sno = student.sno AND sc.fk_cno = course.cno
)
); SELECT * FROM course
WHERE NOT EXISTS
(
SELECT 3>5
); SELECT * FROM course
WHERE EXISTS
(
SELECT 3>5
);
explain
mysql> explain
-> SELECT sname FROM student
-> WHERE
-> (
-> SELECT COUNT(*) FROM
-> (
-> SELECT * FROM (SELECT * FROM sc RIGHT JOIN course ON sc.fk_cno=course.cno) w RIGHT JOIN student ON w.fk_sno=student.sno
-> ) wb
-> WHERE wb.sname = student.sname
-> )
-> =
-> (SELECT COUNT(*) FROM course );
+----+--------------------+---------+------------+--------+----------------+---------+---------+-------------------+------+----------+------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+---------+------------+--------+----------------+---------+---------+-------------------+------+----------+------------------------------+
| 1 | PRIMARY | student | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Using where |
| 5 | SUBQUERY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
| 2 | DEPENDENT SUBQUERY | student | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 33.33 | Using where |
| 2 | DEPENDENT SUBQUERY | sc | NULL | ref | PRIMARY,fk_cno | PRIMARY | 4 | w0811.student.sno | 2 | 100.00 | Using index |
| 2 | DEPENDENT SUBQUERY | course | NULL | eq_ref | PRIMARY | PRIMARY | 12 | w0811.sc.fk_cno | 1 | 100.00 | Using index |
+----+--------------------+---------+------------+--------+----------------+---------+---------+-------------------+------+----------+------------------------------+
5 rows in set, 2 warnings (0.00 sec) mysql>
mysql> explain
-> SELECT sname FROM student
-> WHERE NOT EXISTS
-> (
-> SELECT * FROM course
-> WHERE NOT EXISTS
-> (
-> SELECT * FROM sc
-> WHERE sc.fk_sno = student.sno AND sc.fk_cno = course.cno
-> )
-> );
+----+--------------------+---------+------------+--------+----------------+---------+---------+------------------------------------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------------+---------+------------+--------+----------------+---------+---------+------------------------------------+------+----------+--------------------------+
| 1 | PRIMARY | student | NULL | ALL | NULL | NULL | NULL | NULL | 3 | 100.00 | Using where |
| 2 | DEPENDENT SUBQUERY | course | NULL | index | NULL | PRIMARY | 12 | NULL | 3 | 100.00 | Using where; Using index |
| 3 | DEPENDENT SUBQUERY | sc | NULL | eq_ref | PRIMARY,fk_cno | PRIMARY | 16 | w0811.student.sno,w0811.course.cno | 1 | 100.00 | Using index |
+----+--------------------+---------+------------+--------+----------------+---------+---------+------------------------------------+------+----------+--------------------------+
3 rows in set, 3 warnings (0.00 sec) mysql>
“带入遍历”,“重复循环”,这是上边代码原貌,而两种思路的区别仅仅是让计算机计算时怎样“重复次数”少一点。
http://blog.csdn.net/wolfofsiberian/article/details/39346781
发问:
0-语言只是工具而已,不是解决问题方法的源泉,怎么减少对接语法API的时间?
User-Defined Variables的更多相关文章
- JMeter学习参数化User Defined Variables与User Parameters
偶然发现JMeter中有两个元件(User Defined Variables与User Parameters)很相近,刚开始时我也没注意,两者有什么不同.使用时却发现两者使用场景有些不同,现在小结一 ...
- 【jmeter】参数化User Defined Variables与User Parameters
偶然发现JMeter中有两个元件(User Defined Variables与User Parameters)很相近,刚开始时我也没注意,两者有什么不同.使用时却发现两者使用场景有些不同,现在小结一 ...
- JMeter学习(十)参数化User Defined Variables与User Parameters(转载)
转载自 http://www.cnblogs.com/yangxia-test 偶然发现JMeter中有两个元件(User Defined Variables与User Parameters)很相近, ...
- 【转】JMeter学习参数化User Defined Variables与User Parameters
偶然发现JMeter中有两个元件(User Defined Variables与User Parameters)很相近,刚开始时我也没注意,两者有什么不同.使用时却发现两者使用场景有些不同,现在小结一 ...
- JMeter学习笔记(九) 参数化3--User Defined Variables
3.User Defined Variables 1)添加用户定义的变量 2)添加变量 3)添加HTTP请求,引用变量,格式:${} 4)执行HTTP请求,察看结果树 5)用户定义的变量,优缺点: * ...
- JMeter学习(十二)JMeter学习参数化User Defined Variables与User Parameters
相同点:二者都是进行参数化的. 一.User Defined Variables 1.添加方法:选择“线程组”,右键点击添加-Config Element-User Defined Variables ...
- jmeter 参数化3_User Defined Variables(用户自定义变量)
User Defined Variables: 一般用于Test Plan中不需要随请求迭代的参数设置,如:Host.Port Number 操作路径:Thread Group-->Add-- ...
- Debian Environment Variables
原文:EnvironmentVariables General Environment variables are named strings available to all application ...
- PowerShell Remove all user defined variable in PowerShell
When PS scripts executes, it is possibly create much user defined variables. So, sometimes these var ...
- [Erlang 0113] Elixir 编译流程梳理
注意:目前Elixir版本还不稳定,代码调整较大,本文随时失效 之前简单演示过如何从elixir ex代码生成并运行Erlang代码,下面仔细梳理一遍elixir文件的编译过程,书接上文,从 ...
随机推荐
- js 学习
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- struts2:表单标签续(datetimepicker标签)
18. datetimepicker标签 18.1 拷贝JAR包 在struts-2.x.x压缩包的lib目录中拷贝struts2-dojo-plugin-2.3.8.jar到WEB-INF/lib目 ...
- 使用SharedPreference和对象流存储对象
编写PreferencesUtils工具类可以简单对象(可以缓存对象中所有public的简单属性) 另外研究了Preference存储与直接采用对象文件缓存方式的区别 第一次写文件时 1.效率,直 ...
- 我的Android进阶之旅------>Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
Android 设备的CPU类型通常称为ABIs 问题描写叙述 解决方法 1解决之前的截图 2解决后的截图 3解决方法 4建议 为什么你须要重点关注so文件 App中可能出错的地方 其它地方也可能出错 ...
- 【九天教您南方cass 9.1】01 安装Cad和Cass9.1
同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程 今后会将cass的教程目录定期发布在测量空间中. 我是本节课主讲老师九天. [点击索取cass教程]5元立得 (给客服说暗号:“老王 ...
- 嵌入式Linux内时区配置
一般情况下,嵌入式Linux系统的时区,也是国际事件. 但是我们是属于东八区的时区,所以有一些时候要更改一下 TZ 变量可以配置时区,如下
- c# 使用GDAL处理大图
注意问题: 1.GDAL 使用官网生成好的dll,必须把Bin目录下的dll一并加到执行目录下去,否则会出错. 2. 用环境变量设置引用路径可以避免一大堆dll放一起.代码如下: /// <s ...
- 关于Unity中NGUI的背包实现之Scrollview(基于Camera)
基于UIPanel的scrollview实现方式在移动设备上的性能不如基于camera的方式.因为UIPanel的scrollview实现方式要渲染很多的道具图,性能自然就降低了.如果是用第二个摄像机 ...
- GDC2017 把“现实的天空”在游戏内再现【Forza Horizon 3】的天空表现
原文链接 http://game.watch.impress.co.jp/docs/news/1047800.html 完全表现出现实世界中各种偶然而不可预料的风景! [Forza Horiz ...
- win7下安装Office2010老是出现提示安装MSXML6.10.1129.0,下载官方MSXML后提示安装成功却也安装不了
在注册表中增加以下信息: [HKEY_CLASSES_ROOT\TypeLib\{F5078F18-C551-11D3-89B9-0000F81FE221}][HKEY_CLASSES_ROOT\Ty ...