用于选择与其他产品成分相同的产品的 SQL 查询
我有一个数据库,存储“市场上有售”的产品和“仍在开发中”的产品 位于两个单独的表中(market_product
和 dev_product
)。第三个表(物质)包含所有 产品可由哪些物质制成。其他两个表(marked_product_comp
和 dev_product_comp
) 保持产品成分。
我想选择仍在开发中且由相同成分制成的产品 上市产品。
在以下(简化)示例中,查询必须从 dev_product 表中选择 ID = 2 的产品。
CREATE table market_product (ID SERIAL PRIMARY KEY);
CREATE table dev_product (ID SERIAL PRIMARY KEY);
CREATE table substance (ID SERIAL PRIMARY KEY);
CREATE table market_product_comp (prodID SERIAL, substID SERIAL, PRIMARY KEY(prodID,substID));
CREATE table dev_product_comp (devID SERIAL, substID SERIAL, PRIMARY KEY(devID,substID));
INSERT INTO market_product VALUES (1),(2);
INSERT INTO dev_product VALUES (1),(2);
INSERT INTO substance VALUES (1),(2),(3);
INSERT INTO market_product_comp VALUES (1,1),(1,2),(2,3);
INSERT INTO dev_product_comp VALUES (1,2),(2,1),(2,2);
如何编写这样的查询?
更新:
抱歉,我没有注意到我以含糊的方式提出了我的问题。
我想选择仍在开发中且与至少一种已上市产品具有相同成分的产品。例如,如果有一种由物质 {1,2} 制成的 dev_product
并且只有一种由物质 {1,2,3} 制成的 market_product
,我想丢弃它dev_product
,因为它具有不同的成分。我希望这能澄清。
I have a database that stores products "available on the market" and products "still in development"
in two separate tables (market_product
and dev_product
). A third table (substance) contains all
substances a product can made of. Other two tables (marked_product_comp
and dev_product_comp
)
mantains product compositions.
I want to select products still in development that are made of the same ingredients of
marketed products.
In the following (simplified) example the query must select product with ID = 2 from dev_product table.
CREATE table market_product (ID SERIAL PRIMARY KEY);
CREATE table dev_product (ID SERIAL PRIMARY KEY);
CREATE table substance (ID SERIAL PRIMARY KEY);
CREATE table market_product_comp (prodID SERIAL, substID SERIAL, PRIMARY KEY(prodID,substID));
CREATE table dev_product_comp (devID SERIAL, substID SERIAL, PRIMARY KEY(devID,substID));
INSERT INTO market_product VALUES (1),(2);
INSERT INTO dev_product VALUES (1),(2);
INSERT INTO substance VALUES (1),(2),(3);
INSERT INTO market_product_comp VALUES (1,1),(1,2),(2,3);
INSERT INTO dev_product_comp VALUES (1,2),(2,1),(2,2);
How to write such query?
UPDATE:
Sorry, I haven't noticed I asked my question in an ambiguous way.
I want to select products still in development that have the same composition of at least one marketed product. For example, if there is a dev_product
made by substances {1,2} and only one market_product
made by substances {1,2,3}, I want to discard that dev_product
, because it has a different composition. I hope this clarify.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
下面的解决方案依赖于
COUNT()
忽略 NULL 的事实。我在 MySQL 5.0.75 上对此进行了测试,但它都是 ANSI 标准 SQL,因此它应该适用于任何品牌的 SQL 数据库。
Here's a solution that relies on the fact that
COUNT()
ignores NULLs.I tested this on MySQL 5.0.75, but it's all ANSI standard SQL so it should work on any brand of SQL database.
在
MySQL
中:在
PostgreSQL
中:这些查询都不使用
COUNT(*)
:只要找到一个不匹配的组件即可停止评估整个对。请参阅我的博客中的这些条目以获取说明:
PostgreSQL
,带有完全外部连接
)MySQL
,带有EXISTS
)In
MySQL
:In
PostgreSQL
:Neither from these queries uses
COUNT(*)
: it's enough to find but a single non-matching component to stop evaluating the whole pair.See these entries in my blog for explanations:
PostgreSQL
, withFULL OUTER JOIN
)MySQL
, withEXISTS
)仅选择所有产品物质均用于市场产品的开发产品 ID。
不包括含有未在生产中使用的任何成分的产品。
Select only dev product ids where all the products substances are used in market products.
Excludes products with ANY ingredients not used in production.