Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 具有联接表的多重和条件_Mysql_Sql_Ruby On Rails_Join - Fatal编程技术网

Mysql 具有联接表的多重和条件

Mysql 具有联接表的多重和条件,mysql,sql,ruby-on-rails,join,Mysql,Sql,Ruby On Rails,Join,我试图在一个MySQL查询中找到一条记录,但如果你知道什么,我不能请你看一看并帮助我 products(Table 1) id | title ---------- 1 | Test 1 2 | Test 2 3 | Test 3 nutritions (Table 2) id | product_id | title | evaluation(string) ------------------------------- 1 | 1 | calorie

我试图在一个MySQL查询中找到一条记录,但如果你知道什么,我不能请你看一看并帮助我

products(Table 1)
id  | title
----------
1   | Test 1
2   | Test 2
3   | Test 3

nutritions (Table 2)
id | product_id | title    | evaluation(string)
-------------------------------
1  | 1          | calories | 8
2  | 3          | proteins | 5
3  | 3          | calories | 8
4  | 3          | Sugar    | 9
5  | 2          | calories | 8
6  | 2          | Sugar    | 9

我想在产品表上应用一个过滤器,如下所示

案例1:

(nutritions.title='carries'和nutritions.evaluation='8')和(nutritions.title='Sugar'和nutritions.evaluation='9')

案例2:

(nutritions.title='carries'和nutritions.evaluation='8')和(nutritions.title='Sugar'和nutritions.evaluation='9')和(nutritions.title='proteins'和nutritions.evaluation='5')

预期产出:

products(case 1)                       products(case 2)
id  | title                            id  | title
----------                            --------------
2   | Test 2                           3   | Test 3
3   | Test 3
我尝试了下面的查询,但这需要时间我有超过70k的产品

Product.find_by_sql("
        select `products`.* from products 
            JOIN nutritions where products.id = nutritions.product_id 
                and (nutritions.title = 'calories' and nutritions.evaluation = '8') 
                and nutritions.product_id IN(
                        select product_id 
                        from nutritions 
                        where nutritions.title = 'Sugar' 
                        and nutritions.evaluation = '9'
                        )
            ")

基本上,一列不能同时包含两个或更多的内容,我的意思是
营养。标题
不能是
卡路里

因此,与其使用和使用或

    (nutritions.title = 'calories' AND nutritions.evaluation = '8') 
OR  (nutritions.title = 'Sugar' AND nutritions.evaluation = '9')

基本上,一列不能同时包含两个或更多的内容,我的意思是
营养。标题
不能是
卡路里

因此,与其使用和使用或

    (nutritions.title = 'calories' AND nutritions.evaluation = '8') 
OR  (nutritions.title = 'Sugar' AND nutritions.evaluation = '9')

有几种方法可以做到这一点。一种方法是使用
exists

select p.*
from products p
where exists (select 1
              from nutritions n
              where n.product_id = p.id and
                    n.title = 'calories' and
                    n.evaluation = '8'
             ) and
      exists (select 1
              from nutritions n
              where n.product_id = p.id and
                    n.title = 'Sugar' and
                    n.evaluation = '9'
             ) ;

有了关于营养(产品id、标题、评价)的索引,这应该有很好的表现。

有几种方法可以实现这一点。一种方法是使用
exists

select p.*
from products p
where exists (select 1
              from nutritions n
              where n.product_id = p.id and
                    n.title = 'calories' and
                    n.evaluation = '8'
             ) and
      exists (select 1
              from nutritions n
              where n.product_id = p.id and
                    n.title = 'Sugar' and
                    n.evaluation = '9'
             ) ;

营养(产品id、标题、评价)的索引应具有非常好的性能。

感谢您的建议,但我想要完全相同的产品,因此我必须为您和条件提供。感谢您的建议,但我想要完全相同的产品,因此我必须为您和条件提供。