我无法理解如何编写特定的 MySQL 查询来执行关系除法/相交。我查看了以下链接/ SO问题,但我仍然感到困惑:
MySQL 相当于 IN() 子句执行逻辑与;关系代数:除法;分开我们站立;高级 SQL:相交;在 MySQL 中执行 INTERSECT 和 MINUS 以及在 MySQL 中替代INTERSECT
我试图调整“MySQL 中的 INTERSECT 的替代方案”中显示的 SQL 查询,但在实现中迷失了方向。
伪代码:
Want : package.* from package WHERE package.pkid equalto package_products.pkid AND package_products.pid is_all_of (
WANT : option_products.pid FROM option_products
WHERE option_products.oid = [optionID]
), // returning packages that have all of the product IDs (pids) and not just one of them.
[OptionID] = $option
MySQL(内部 db_query() :
"SELECT package.* FROM package WHERE package.pkid = package_products.pkid && package_products.pid is_allof(".
// What do I use in place of 'is_all_of'? How do I do an intersection/ relational division with INNER JOIN and/ or USING(pid)?
" SELECT options_products.pid FROM options_products WHERE options_products.oid = :option
)", array(":option" => $option )