Mysql 何处存在具有内部Where条件的SQL?

Mysql 何处存在具有内部Where条件的SQL?,mysql,sql,Mysql,Sql,我有以下要求,更准确地说是SQL参数化查询: select * from `products` where (exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and exists (select * from `productprices` where `products`.`Id` = `product

我有以下要求,更准确地说是SQL参数化查询:

select * from `products` 
where (exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and 
exists (select * from `productprices` where `products`.`Id` = `productprices`.`Products_Id` and `Price` >= ?) 
and `Organization_Id` = ? and `name` like ? or `Description` like ?) and `Status` = ?
此查询仅检索存在
国家/地区
价格
的产品,使用运算符
查找存在的地区

如果找到它,它将为第一个子查询返回
TRUE
,然后处理剩余的两个查询
WHERE
。第一个子查询返回
TRUE
,第二个子查询返回
TRUE
,因为子查询也是正确的<代码>结果=真*真=真。但这是错误的结果

问题是,需要使用两个实习生查询WHERE for result from
WHERE EXISTS


这是否意味着我需要在加入的上替换存在的?或者可以修改上面的查询吗?

这样不好吗?试试这个:

select * from `products` inner join productprices on `products`.`Id` = `productprices`.`Products_Id`
INNER JOIN `productslocation` ON `products`.`Id` = `productslocation`.`Product_Id`
where (`Organization_Id` = ? and `name` like ? or `Description` like ?) and `Status` = ? 
AND `productprices`.`Price` >= ?
and `productslocation`.Country_Id` = ?
    select * from `products` 
    where 
    (
    exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and 
    exists (select * from `productprices` where `products`.`Id` = `productprices`.`Products_Id` and `Price` >= ?) and 
    `Organization_Id` = ? and (`name` like ? or `Description` like ?) and `Status` = ?
    )

那样不是更好吗?试试这个:

select * from `products` inner join productprices on `products`.`Id` = `productprices`.`Products_Id`
INNER JOIN `productslocation` ON `products`.`Id` = `productslocation`.`Product_Id`
where (`Organization_Id` = ? and `name` like ? or `Description` like ?) and `Status` = ? 
AND `productprices`.`Price` >= ?
and `productslocation`.Country_Id` = ?
    select * from `products` 
    where 
    (
    exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and 
    exists (select * from `productprices` where `products`.`Id` = `productprices`.`Products_Id` and `Price` >= ?) and 
    `Organization_Id` = ? and (`name` like ? or `Description` like ?) and `Status` = ?
    )

您遗漏了或声明中的括号:

select * from `products` 
where (exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and 
exists (select * from `productprices` where `products`.`Id` = `productprices`.`Products_Id` and `Price` >= ?) 
and `Organization_Id` = ? and (`name` like ? or `Description` like ?)) and `Status` = ?

您遗漏了或声明中的括号:

select * from `products` 
where (exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and 
exists (select * from `productprices` where `products`.`Id` = `productprices`.`Products_Id` and `Price` >= ?) 
and `Organization_Id` = ? and (`name` like ? or `Description` like ?)) and `Status` = ?

您正在寻找一个查询,该查询根据各种条件过滤
产品
,有些条件涉及引用关系

假设您与参考表有
1-1
关系,您应该能够使用
JOIN
s:

select *
from 
    `products` 
    inner join `productslocation` 
        on `products`.`Id` = `productslocation`.`Product_Id` and `productslocation`.`Country_Id` = ?
    inner join `productprices` 
        on  `products`.`Id` = `productprices`.`Products_Id` and `productprices`.`Price` >= ?
where
    `products`.`Organization_Id` = ? 
    and `products`.`name` like ? 
    and `products`.`Description` like ?
    and `products`.`Status` = ?

您正在寻找一个查询,该查询根据各种条件过滤
产品
,有些条件涉及引用关系

假设您与参考表有
1-1
关系,您应该能够使用
JOIN
s:

select *
from 
    `products` 
    inner join `productslocation` 
        on `products`.`Id` = `productslocation`.`Product_Id` and `productslocation`.`Country_Id` = ?
    inner join `productprices` 
        on  `products`.`Id` = `productprices`.`Products_Id` and `productprices`.`Price` >= ?
where
    `products`.`Organization_Id` = ? 
    and `products`.`name` like ? 
    and `products`.`Description` like ?
    and `products`.`Status` = ?

你的括号和
s和
s使陈述变得非常复杂
试试这个:

select * from `products` inner join productprices on `products`.`Id` = `productprices`.`Products_Id`
INNER JOIN `productslocation` ON `products`.`Id` = `productslocation`.`Product_Id`
where (`Organization_Id` = ? and `name` like ? or `Description` like ?) and `Status` = ? 
AND `productprices`.`Price` >= ?
and `productslocation`.Country_Id` = ?
    select * from `products` 
    where 
    (
    exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and 
    exists (select * from `productprices` where `products`.`Id` = `productprices`.`Products_Id` and `Price` >= ?) and 
    `Organization_Id` = ? and (`name` like ? or `Description` like ?) and `Status` = ?
    )

你的括号和
s和
s使陈述变得非常复杂
试试这个:

select * from `products` inner join productprices on `products`.`Id` = `productprices`.`Products_Id`
INNER JOIN `productslocation` ON `products`.`Id` = `productslocation`.`Product_Id`
where (`Organization_Id` = ? and `name` like ? or `Description` like ?) and `Status` = ? 
AND `productprices`.`Price` >= ?
and `productslocation`.Country_Id` = ?
    select * from `products` 
    where 
    (
    exists (select * from `productslocation` where `products`.`Id` = `productslocation`.`Product_Id` and `Country_Id` = ?) and 
    exists (select * from `productprices` where `products`.`Id` = `productprices`.`Products_Id` and `Price` >= ?) and 
    `Organization_Id` = ? and (`name` like ? or `Description` like ?) and `Status` = ?
    )

我使用WHERE EXISTS按属性过滤产品,这非常方便,但是当我想过滤表本身中的结果数据时,它给出了错误的WHERE EXISTS的结果对应逻辑。在我的例子中,似乎是
WHERE Organization\u Id
=?和
name
类似吗?或者
Description
like?`应该放在每个子查询中,不是吗?您的EXISTS语句看起来不错。我认为您的问题在于,您在products表本身的WHERE子句中混合了AND和OR语句。每当我使用或时,我立即开始使用括号来澄清。我的猜测是,处理AND和OR语句的优先顺序会给您带来意想不到的结果。我使用WHERE EXISTS按属性筛选产品,这非常方便,但当我要筛选表本身中的结果数据时,它给出了错误的WHERE EXISTS对应逻辑的结果。在我的例子中似乎是
WHERE组织\u Id
=?和
name
类似吗?或者
Description
like?`应该放在每个子查询中,不是吗?您的EXISTS语句看起来不错。我认为您的问题在于,您在products表本身的WHERE子句中混合了AND和OR语句。每当我使用或时,我立即开始使用括号来澄清。我猜处理AND和OR语句的优先顺序会给你带来意想不到的结果。你知道当我使用一些where时如何用括号写这个吗?不,从来没有处理过Laravel。你知道当我使用一些where时如何用括号写这个吗?不,从来没有处理过Laravel。