【问题标题】:How to SELECT multiple rows with condition that applies to multiple columns in those rows?如何选择多行,条件适用于这些行中的多列?
【发布时间】:2014-06-19 02:35:41
【问题描述】:

我有一张这样的桌子:

  Users:
--------------------------------------------------------------------------------
    User_id: User_name: User_email: User_has_neck: User_has_eyes: User_has_head: User_has_hair
    1      | John     | jon@jk.com| yes          | yes          |  sometimes   |  sometimes
    2      | Kate     | kte@jk.com| yes          | yes          |  sometimes   |  some
    3      | Mark     | mrk@jk.com| yes          | sometimes    |  sometimes   |  sometimes
    4      | Kora     | sometimes | yes          | yes          |  yes         |  some
    5      | Acts     | act@jk.com| sometimes    | sometimes    |  sometimes   |  some
    6      | Jerw     | sometimes | yes          | yes          |  yes         |  too_much
    7      | Dude     | sometimes | sometimes    | sometimes    |  yes         |  too_much
    8      | Ninja    | nja@kl.com| yes          | yes          |  yes         |  too_much

现在,我需要像这样查询:

SELECT * FROM Users WHERE <NO_COLUMN_IS_EQUAL_TO_sometimes>; 

一列可能有如下数据; 'yes','some'NOT 'sometimes';

如何做到这一点?...任何建议都非常感谢。

我试过了,

SELECT * FROM Users WHERE *#@!$%^()**#$ NO IDEA..... '--------->stackOverflow'

【问题讨论】:

  • 您确定表中有多少列吗?还是未知数?
  • 我不介意投反对票....但是请让我知道为什么我会受到惩罚...(请在投反对票之前发表评论,我请求。谢谢大师!!!)
  • @shree.pat18 当然,问题是行可能有超过 54COLUMNS...这就是为什么我正在寻找一种方法来完成此操作而不使用列...thx

标签: mysql select where rows


【解决方案1】:

只需在where 子句中进行显式比较即可。这是一个例子:

SELECT *
FROM Users
WHERE 'sometimes' not in (User_has_neck, User_has_eyes, User_has_head, User_has_hair);

编辑:

在您的示例中,您可以使用以下查询生成列表:

select group_concat(column_name separator ', ')
from information_schema.columns c
where table_name = 'users' and table_schema = ???;

【讨论】:

  • 感谢您的建议,不过,这些行可能有超过 54 列...这就是为什么我正在寻找一种方法来完成此操作而不使用列...再次感谢
  • @UniversalGrasp 所以你必须做一些打字。您可以编写通过信息模式生成此列表的代码。
猜你喜欢
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2018-01-15
  • 1970-01-01
  • 2022-09-30
相关资源
最近更新 更多