草庐IT

多行条件下的mysql不同列

coder 2023-10-11 原文

我有一个带有下表的 mysql 数据库 (item_preset):

+-----+-----------+---------+-------+ 
| id  | preset_id | item_id | value |
+-----+-----------+---------+-------+
| 1   | 1         | 1       | 2     | 
| 2   | 1         | 2       | 1     | 
| 3   | 1         | 4       | 60    | 
| 4   | 1         | 3       | 16    | 
| 5   | 1         | 3       | 17    | 
| 6   | 1         | 3       | 18    |
| 7   | 1         | 3       | 25    |
| 8   | 1         | 3       | 26    | 
| 9   | 1         | 3       | 27    | 
| 10  | 2         | 1       | 3     | 
| 11  | 2         | 2       | 0     | 
| 12  | 2         | 4       | 0     | 
| 13  | 2         | 3       | 16    | 
| 14  | 2         | 3       | 17    | 
| 15  | 2         | 3       | 19    | 
| 16  | 2         | 3       | 20    | 
| 17  | 2         | 3       | 21    | 
| 18  | 3         | 1       | 2     | 
| 19  | 3         | 2       | 0     | 
| 20  | 3         | 4       | 0     | 
| 21  | 3         | 3       | 25    | 
| 22  | 3         | 3       | 28    | 
| 23  | 4         | 1       | 1     | 
| 24  | 4         | 2       | 1     | 
| 25  | 4         | 4       | 120   | 
| 26  | 4         | 3       | 16    | 
| 27  | 4         | 3       | 17    |         
| 28  | 4         | 3       | 18    | 
| 29  | 4         | 3       | 22    | 
| 30  | 4         | 3       | 23    | 
| 31  | 4         | 3       | 24    | 
| 32  | 6         | 1       | 3     | 
| 33  | 6         | 2       | 1     | 
| 34  | 6         | 4       | 90    | 
| 35  | 6         | 3       | 18    | 
| 36  | 6         | 3       | 22    | 
| 37  | 6         | 3       | 23    | 
| 38  | 6         | 3       | 24    | 
| 39  | 6         | 3       | 29    | 
| 40  | 6         | 3       | 30    | 
+-----+-----------+---------+-------+

我想做的是根据来自多行的条件获取不同的 preset_id。 例如,要获得 preset_id 1,我需要所有条件都为真(item_id = 1 和 value_id = 2),(item_id = 2 和 value = 1),等等......

我试过使用以下方法: 从 item_preset 中选择不同的 preset_id,其中 (item_id = 1 and value = 2) and (item_id = 2 and value = 1) and (item_id = 4 and value = 60);

但得到一个空集。如果我尝试使用 Or 而不是,我会得到所有符合任何条件的 preset_id。

有什么想法吗?

谢谢

最佳答案

你可能想试试这个东西,

SELECT   preset_id
FROM    tableName
WHERE   (item_id = 1 and value = 2) OR 
        (item_id = 2 and value = 1) OR
        (item_id = 4 and value = 60)
GROUP BY preset_id
HAVING COUNT(*) = 3

SQLFiddle Demo

关于多行条件下的mysql不同列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12926135/

有关多行条件下的mysql不同列的更多相关文章

  1. ruby-on-rails - 如何在 ruby​​ 交互式 shell 中有多行? - 2

    这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式ruby​​shell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f

  2. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  3. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  4. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  5. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  6. ruby-on-rails - 使用包含多个关联和单独的条件 - 2

    我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS

  7. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  8. ruby-on-rails - 在具有 ActiveRecord 条件的相关模型中按字段排序 - 2

    我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我

  9. ruby-on-rails - 无法安装 mysql2 0.3.14 gem - 2

    我看到其他人也遇到过类似的问题,但没有一个解决方案对我有用。0.3.14gem与其他gem文件一起存在。我已经完全按照此处指示完成了所有操作:https://github.com/brianmario/mysql2.我仍然得到以下信息。我不知道为什么安装程序指示它找不到include目录,因为我已经检查过它存在。thread.h文件存在,但不在ruby​​目录中。相反,它在这里:C:\RailsInstaller\DevKit\lib\perl5\5.8\msys\CORE\我正在运行Windows7并尝试在Aptana3中构建我的Rails项目。我的Ruby是1.9.3。$gemin

  10. ruby - 如果满足给定条件,则结束 ruby​​ 程序 - 2

    基本上,我只是试图在满足特定条件时停止程序运行其余行。unlessraw_information.firstputs"Noresultswerereturnedforthatquery"breakend然而,在程序运行之前我得到了这个错误:Invalidbreakcompileerror(SyntaxError)执行此操作的正确方法是什么? 最佳答案 abort("Noresultswerereturnedforthatquery")unlesscondition或unlessconditionabort("Noresultswer

随机推荐