我有以下数据库设计。
产品
+----+----------------------------------+
| id | name |
+----+----------------------------------+
| 1 | Product 1, Intel, Celeron, N3350 |
| 2 | Product 2, Intel, Celeron, N3350 |
| 3 | Product 3, Intel, i5, 8250U |
| 4 | Product 4, Intel, i9, 8950HK |
| 5 | Product 5, Intel, i9, 8950HK |
+----+----------------------------------+
属性
+----+--------------+
| id | name |
+----+--------------+
| 11 | Manufacturer |
| 22 | Type |
| 33 | Model |
+----+--------------+
ATTRIBUTE_VALUES
+-----+---------+
| id | value |
+-----+---------+
| 111 | Intel |
| 222 | Celeron |
| 333 | i5 |
| 444 | i9 |
| 555 | N3350 |
| 666 | 8250U |
| 777 | 8950HK |
+-----+---------+
最后,链接所有这些的表格:
PRODUCT_ATTRIBUTES_VALUES
+------------+--------------+--------------------+
| product_id | attribute_id | attribute_value_id |
+------------+--------------+--------------------+
| 1 | 11 | 111 |
| 1 | 22 | 222 |
| 1 | 33 | 555 |
| 2 | 11 | 111 |
| 2 | 22 | 222 |
| 2 | 33 | 555 |
| 3 | 11 | 111 |
| 3 | 22 | 333 |
| 3 | 33 | 666 |
| 4 | 11 | 111 |
| 4 | 22 | 444 |
| 4 | 33 | 777 |
| 5 | 11 | 111 |
| 5 | 22 | 444 |
| 5 | 33 | 777 |
+------------+--------------+--------------------+
所有这些都是关于过滤器产品。
我想返回每个属性过滤器的结果。
如果我过滤:
SELECT products.id, products.name FROM `products` LEFT JOIN
`product_attributes_values` ON
`product_attributes_values`.`product_id`=`products`.`id` LEFT JOIN
`attributes` ON
`attributes`.`id`=`product_attributes_values`.`attribute_id` LEFT JOIN
`attribute_values` ON
`attribute_values`.`id`=`product_attributes_values`.`attribute_value_id`
WHERE ((attributes.id = 11 AND `attribute_values`.`id` IN (111)) OR
(`attributes`.`id` = 22 AND `attribute_values`.`id` IN (222)) ) GROUP
BY `products`.`id` LIMIT 10
最佳答案
试试这个
SELECT
products.id,
products.name
FROM
`products`
LEFT JOIN `product_attributes_values`
ON `product_attributes_values`.`product_id`=`products`.`id`
LEFT JOIN `attributes`
ON `attributes`.`id`=`product_attributes_values`.`attribute_id`
LEFT JOIN `attribute_values`
ON `attribute_values`.`id`=`product_attributes_values`.`attribute_value_id`
WHERE
attributes.id = 11
OR attributes.id = 22
GROUP BY
products.id,
products.name
HAVING
MAX(CASE WHEN attributes.id = 11 THEN attribute_values.id ELSE NULL END) IN (111)
AND MAX(CASE WHEN attributes.id = 22 THEN attribute_values.id ELSE NULL END) IN (222)
LIMIT
10
如果你知道你所有的id,那么你就不需要所有这些连接
SELECT
p.id,
p.name
FROM
`products` as p
LEFT JOIN `product_attributes_values` as pav1
ON p.id = pav1.product_id
and pav1.attribute_id = 11
LEFT JOIN `product_attributes_values` as pav2
ON p.id = pav2.product_id
and pav2.attribute_id = 22
WHERE
pav1.attribute_value_id = 111
AND pav2.attribute_value_id = 222
关于mysql - 从最后一个 WHERE 条件获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57287786/
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano