简体   繁体   English

至少两个匹配字段(mysql)

[英]At least two matching fields (mysql)

I've got a query like: 我有一个查询,如:

SELECT * FROM table WHERE id=1 AND (field1=1 OR field2=1 OR field3=1 OR field4=1 OR field5=1 OR field6=1 OR field7=1 OR field8=1)

The actual query is longer and there are more than 25 fields in the brackets... 实际查询时间更长,方括号中的字段超过25个...

How would I make sure that at least two of the fields = 1? 如何确定至少两个字段= 1? If those were rows it would be easy, but what about columns? 如果这些是行,那将很容易,但是列呢? Not sure if it's even possible. 不知道是否有可能。

Basically I need to run a php function only if there are at least two matching fields. 基本上,只有在至少有两个匹配字段时,才需要运行php函数。 The function then updates the selected fields so that they will not be selected again next time. 然后,该功能将更新选定的字段,以便下次不再选择它们。

In MySQL, conditional statements can be evaluated directly, and they return "1" if they are true and "0" if they are false. 在MySQL中,条件语句可以直接求值,如果为true,则返回“ 1”,如果为false,则返回“ 0”。 So in this case, you can just sum the conditionals like so: 因此,在这种情况下,您可以像这样对条件进行求和:

SELECT * FROM table WHERE id=1 AND (field1=1) + (field2=1) + (field3=1) + (...) >= 2

If the sum is equal to or greater than 2 then at least two fields have the value "1". 如果总和等于或大于2,则至少两个字段的值为“ 1”。 Note that you must put parenthesis around the conditionals here to make sure they are individually evaluated; 请注意,您必须在此处加上条件,以确保对它们进行单独评估。 otherwise it doesn't parse correctly. 否则无法正确解析。

In the general case, there is also the MySQL's IF function. 在一般情况下,还有MySQL的IF函数。 You could use it here like this: 您可以在这里像这样使用它:

SELECT * FROM table WHERE id=1 AND IF(field1=1, 1, 0) + IF(field2=1, 1, 0) + IF(field3=1, 1, 0) + (...) >= 2

IF() takes three arguments. IF()接受三个参数。 The first is a condition. 首先是条件。 The second is the return value if the condition is true. 如果条件为真,则第二个为返回值。 The third is the return value if the condition is false. 第三个是条件为false时的返回值。 So this would work too since in MySQl, the value of a bare conditional is equivalent to the value of IF(conditional, 1, 0) . 因此这也是可行的,因为在MySQl中,裸条件的值等于IF(conditional, 1, 0)

Perhaps you could add another column or another table through a relation which would contain a count of those fields which contain the 1 value. 也许您可以通过一个关系添加另一列或另一个表,该关系将包含那些包含1值的字段的计数。 Obviously there would then have to be some INSERT/UPDATE logic which either did this on every update to your existing table or was performed in a transaction: BEGIN UPDATE ... ; 显然,必须有一些INSERT / UPDATE逻辑,它们要么在对现有表的每次更新中都执行此操作,要么在事务中执行:BEGIN UPDATE ...; SELECT FROM table WHERE id=1 and fields_containing_one > 2; SELECT FROM table WHERE id = 1和fields_ contains_one> 2; COMMIT (to insure isolation from any racing updates or inserts between your UPDATE logic and your desired query. COMMIT(以确保与任何快速更新或UPDATE逻辑与所需查询之间的插入隔离)。

I'm tacitly assuming that your fieldN values can be numbers other than 0 and 1 and that you are looking only for those which are precisely == 1. If you know that the fieldN values are only 0 or 1 then you could probably work with just a SUM of all the fieldN values. 我默认您的fieldN值可以是0和1以外的数字,而您只在寻找精确地== 1的数字。如果您知道fieldN值仅是0或1,那么您可以使用只是所有fieldN值的总和。

Overall it sounds like your table/schema is denormalized and that's likely to cause other issues as you continue to work with this database. 总体来说,这听起来像是您的表/架构被非规范化,并且在您继续使用该数据库时,很可能会导致其他问题。 Rather than having field1, field2, ... for each row of your data set you could probably normalize the schema by having two columns (field_num or attribute_name and the corresponding value). 不必为数据集的每一行都设置field1,field2,...,而是可以通过具有两列(field_num或attribute_name以及相应的值)来规范化架构。 Then instead of one row with N fields for a given entity you'd have N rows for each entity ... one for every (KEY)+(fieldN,value) tuple. 然后,对于给定实体,没有一行具有N个字段,而是每个实体都有N行...每个(KEY)+(fieldN,value)元组一个。

Under that schema you might add the constraint UNIQUE (whatever your existing key is + field_name) (to ensure that each entity has no more than one of each type of "field_name" rows). 在该模式下,您可以添加约束UNIQUE(无论现有键是+ field_name)(以确保每个实体最多只能有一种“ field_name”行类型)。

If that were your schema then you could do something like: SELECT id FROM (SELECT table WHERE id=1 AND value=1) WHERE COUNT(id) > 1 (in other words use a sub-SELECT clause in your query. 如果这是您的架构,则可以执行以下操作:SELECT id FROM(SELECT表WHERE id = 1 AND value = 1)WHERE COUNT(id)> 1(换句话说,在查询中使用sub-SELECT子句)。

通过将fields ... 8加在一起,看看结果是否等于2应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM