Advanced SQL: Relational division in jOOQ
Relational algebra has its treats. One of the most academic features is the relational division. It is hardly ever used, but comes in handy every now and then. And when you need it, you’ll probably hate yourself for having slept during the relevant classes at the university.
What is relational division?
Relational division is the inverse of a cross join operation. The following is an approximate definition of a relational division:
Assume the following cross join / cartesian product
C = A × B Then it can be said that
A = C ÷ B
B = C ÷ A
What does it mean, typically?
Let’s have a look at the sample provided on Wikipedia:
Wikipedia example of a relational division
This looks sensible. The division of Completed ÷ DBProject leads to a list of students that have completed all projects.
Now how to phrase that in SQL??
That’s not so simple as it looks. The most commonly documented solution involves a doubly-nested select statement using anti-joins. In human language (using double negative), it is like Fred and Sarah saying “there is no DBProject that we have not Completed“. Or in SQL:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
 | 
SELECT DISTINCT "c1".Student FROM Completed "c1"WHERE NOT EXISTS (  SELECT 1 FROM DBProject  WHERE NOT EXISTS (    SELECT 1 FROM Completed "c2"    WHERE "c2".Student = "c1".Student    AND "c2".Task = DBProject.Task  )) | 
Now, no one sane wants to remember this just for the 1-2 times in a SQL developer’s life that they actually need it. So they use jOOQ, which wraps up the above monster in a concise syntax:
| 
 1 
2 
3 
4 
5 
 | 
create.select().from(  Completed.divideBy(DBProject)           .on(Completed.Task.equal(DBProject.Task))           .returning(Completed.Student)); | 
Note that from the above SQL statement, it is immediately clear that proper indexing is of the essence. Be sure to have indexes on all columns referenced from the on(…) and returning(…) clauses.
More information
For more information about relational division and some nice, real-life examples, see
Advanced SQL: Relational division in jOOQ的更多相关文章
- Advanced SQL
		
Top number of records SELECT column_name FROM table_name LIMIT 5; Like/Not Like SELECT * FROM Custom ...
 - 转载:10 Easy Steps to a Complete Understanding of SQL
		
10 Easy Steps to a Complete Understanding of SQL 原文地址:http://tech.pro/tutorial/1555/10-easy-steps-to ...
 - 10 Easy Steps to a Complete Understanding of SQL
		
原文出处:http://tech.pro/tutorial/1555/10-easy-steps-to-a-complete-understanding-of-sql(已经失效,现在收集如下) Too ...
 - 实例讲解 SQL 注入攻击
		
这是一篇讲解SQL注入的实例文章,一步一步跟着作者脚步探索如何注入成功,展现了一次完整的渗透流程,值得一读.翻译水平有限,见谅! 一位客户让我们针对只有他们企业员工和顾客能使用的企业内网进行渗透测试. ...
 - PHP+MYSQL网站SQL Injection攻防
		
程序员们写代码的时候讲究TDD(测试驱动开发):在实现一个功能前,会先写一个测试用例,然后再编写代码使之运行通过.其实当黑客SQL Injection时,同样是一个TDD的过程:他们会先尝试着让程序报 ...
 - HP+MYSQL网站SQL Injection攻防
		
WebjxCom提示:程序员们写代码的时候讲究TDD(测试驱动开发):在实现一个功能前,会先写一个测试用例,然后再编写代码使之运行通过.其实当黑客SQL Injection时,同样是一个TDD的过程: ...
 - SQL注入备忘单
		
Find and exploit SQL Injections with free Netsparker SQL Injection Scanner SQL Injection Cheat Sheet ...
 - jOOQ
		
jOOQ http://www.jooq.org/ jOOQ是个更不错的SQL解决方案. 你可以在Java中以一种类型安全的方式来书写SQL语句: // Typesafely execute the ...
 - Qt SQL Programming 部分翻译
		
简介: Qt SQL 是 Qt 的重要模块之一,为了方便,Qt 对 SQL 进行了一系列的封装,并将 SQL API 分为如下三层: (1)驱动层 (2)SQL API ...
 
随机推荐
- Webshell清除-解决驱动级文件隐藏挂马
			
Webshell清除-解决驱动级文件隐藏挂马
 - JavaScript中对事件简单的理解
			
事件(event) 1.什么是JavaScript事件? 事件是文档或浏览器中发生的特定交互瞬间. 2.事件流 事件流描述的是从页面中接受事件的顺序,包含IE提出的事件冒泡流与Netscape提出的事 ...
 - 【python】-- web框架本质
			
web框架 一.web框架简述 所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. import socket def handle_request( ...
 - GPU instancing
			
参考 https://www.cnblogs.com/hont/p/7143626.html github地址 https://github.com/yingsz/instancing/ 补充2点: ...
 - Linux查找命令find、locate、whereis、which、type
			
find:查找指定下目录的文件 -empty:查找空文件 -name:按名字查找 -type f(文件 or d:目录):按类型查找
 - xlwt 模块 操作excel
			
1.xlwt 基本用法 import xlwt #1 新建文件 new_file = open('test.xls', 'w') new_file.close() #2 创建工作簿 wookbook ...
 - 启动 nodemanger 报错javax.security.sasl.SaslException: GSS initiate failed
			
最近启动 Hadoop, nodemanger 老挂,报kerberos 验证错误,各种查找原因,时间也同步,kint 也能登录到kerberos,一直找不到原因,最后发现是网关和远端的时间同步,但是 ...
 - ubuntu ping响应慢的解决(转)
			
新装ubuntu之后感觉上网老是很慢,ping网站时每次ping指令都需要很久才能有响应,不过网络延迟却正常.后来发现是因为/etc/nsswitch.conf文件中hosts的配置有问题,做如下修改 ...
 - 第K层的结点数
			
int GetNodeNumKthLevel(BiTNode * pRoot, int k) { if(pRoot == NULL || k < 1) return 0; if(k == 1) ...
 - SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传
			
SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...