0

我无法理解如何编写特定的 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 )
4

1 回答 1

0

也许我在这里遗漏了一些东西,但我会这么说

db_query("SELECT package.* FROM package 
WHERE package.pkid = package_products.pkid 
&& package_products.pid IN 
(SELECT options_products.pid FROM options_products 
WHERE options_products.oid = %d)", $option));

应该为你工作。

我已经重写了您的语法以满足 Drupal 6 db_query 要求(http://api.drupal.org/api/drupal/includes!database.mysql-common.inc/function/db_query/6)。

于 2013-05-02T13:08:32.230 回答