php - mysql 查询 - 排除一个条目

标签 php mysql database

我有 2 个表,其中 1 个表是 order,另一个表是 order_itemsOrder_items 包含来自 order 的 order_id 的许多记录。我想查询表顺序,以便订单包含某些 order_items(例如 product_item = 'nameProduct')。它会将这些订单排除在结果之外。我该怎么做?

我当前的查询是:

select * orders where order_id in (select order_id from order_items where product_item !='nameProduct');

此查询实际上不起作用,因为select order_id from order_items where product_item !='nameProduct'仍然可以选择具有相同order_id但只是具有不同的条目>产品项目

提前致谢!

最佳答案

SELECT  *
FROM    orders o
WHERE   NOT EXISTS ( SELECT *
                     FROM   order_items oi
                     WHERE  product_item = 'nameProduct'
                            AND oi.order_id = o.order_id )

关于php - mysql 查询 - 排除一个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7184001/

相关文章:

php - 错误 "Too few arguments to function"

php - 处理 php xml 帖子和批处理

java - SQLite,如何获取数据库中的所有表名?

php - 为什么我在 PHP/MySQL 中收到错误 SQLSTATE42000

mysql - 使用 where 子句更新 MySQL

sql - 多选插入

database - 使用 SQLWorkbench 列出 redshift 中的表

php - 如何从两个不同的表中获取字段并使用 WHERE 检查值?

php - pdo 检索数据并填充记录

php - 如何在连续文本区域中打印结果?