php - 选择具有不同放置可能组合的字段值

标签 php mysql sql select

我想在包含 item1、2、3、4、5 的不同可能组合中选择字段值。项目放在哪里并不重要,但每个字段必须包含每个项目。

id_num    field1       field2   field3   field4   field5
  1        item1       item2    item3    item4     item5   > true
  2        item3       item3    item3    item4     item5   > false
  3        item2       item1    item3    item4     item5   > true
  4        item5       item4    item3    item2     item1   > true
  5        item1       item1    item3    item4     item5   > false
  6        item2       item2    item4    item4     item5   > false

我尝试使用常规查询 SELECT * from tb_name where field1=item1 AND field2=item2 etc... OR OR AND OR 一定很耗电,效率肯定不是很高。

谢谢

最佳答案

您可以使用 field 函数使您的查询比一堆 ands 和 ors 稍微简单一些:

SELECT * 
FROM   tb_name
WHERE  FIELD ('item1', field1, field2, field3, field4, field5) > 0 AND
       FIELD ('item2', field1, field2, field3, field4, field5) > 0 AND
       FIELD ('item3', field1, field2, field3, field4, field5) > 0 AND
       FIELD ('item4', field1, field2, field3, field4, field5) > 0 AND
       FIELD ('item5', field1, field2, field3, field4, field5) > 0

关于php - 选择具有不同放置可能组合的字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27863709/

相关文章:

php - Robots.txt优先级问题

android - 将 MySQL 数据库导入到 android SQLite 数据库

php - 将 SQL 查询转换为子查询

Mysql 报告 - 无法获得匹配的数字

php - Sphinx 要求,它可以在我的 Webhostings 提供商上运行吗?

php - 类型错误 : "Cannot convert string to ByteString because the character at index 519 has value 8230 which is greater than 255."

php - 如何在 PHP MYSQL 中缓存 RAND() 随机图像

mysql - 如何使用 Mysql 更新特定列中除一个条目之外的所有条目

c# - SQL 垂直显示表

SQL 在 Where 语句中使用别名