SQL 查询选择 where A='foo'其中 (A=“bar” 和 B=“baz”)

发布于 2024-09-07 22:08:55 字数 430 浏览 1 评论 0原文

我有下一个 SQL 查询:

SELECT FIRST_NAME, LAST_NAME
FROM MYTABLE
WHERE STATUS = 1 AND VARIABLE IN ( 'PR', 'AR' ) AND SPECIAL = 1;

SPECIAL 是一个附加条件,如果我之前在表单中选择了“PR”(复选框),则会添加该条件。问题是,通过这个查询,我不再获得 VARIABLE 为“AR”的行。

现在,这个想法是,当 VARIABLE 为“PR”或“AR”时,此查询应返回任何行,但如果它是“PR”,则来自此仅“PR”的组,它应仅返回 特殊。我的意思是,它仍然应该显示 VARIABLE = 'AR' 的行,但如果 SPECIAL = 1,那么“PR”的行应该只是那些 SPECIAL = 1 的行。

I've got the next SQL query:

SELECT FIRST_NAME, LAST_NAME
FROM MYTABLE
WHERE STATUS = 1 AND VARIABLE IN ( 'PR', 'AR' ) AND SPECIAL = 1;

SPECIAL is an additional condition which is added if I previously selected 'PR' (checkbox) in my form. The thing is, with this query, I'm no longer getting rows where VARIABLE is 'AR'.

Now, the idea is that this query should return any rows when VARIABLE is 'PR' or 'AR', but if it is 'PR', from this 'PR'-only group, it should return only the SPECIAL ones. I mean, it should still display rows with VARIABLE = 'AR', but if SPECIAL = 1, then the ones that are 'PR', should be only those where SPECIAL = 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

两人的回忆 2024-09-14 22:08:55

由于这是一项作业,我认为最好解释一下如何做,而不是仅仅给您复制和粘贴的代码。

您想要选择 STATUS = 1 的行以下之一:

  • VARIABLE 为“AR”

  • VARIABLE 为“PR” SPECIAL = 1.

使用ANDOR 的组合。请记住,AND 的优先级高于 OR。最好使用括号来明确如何计算表达式。

Since it is an assignment I think it is better to explain how to do it than to just to give you the code to copy and paste.

You want to select a row where STATUS = 1 and one of:

  • VARIABLE is 'AR'

or:

  • VARIABLE is 'PR' and SPECIAL = 1.

Use a combination of AND and OR. Remember that AND has higher precedence than OR. It is a good idea to use parentheses to be explicit about how the expression should be calculated.

美胚控场 2024-09-14 22:08:55
SELECT FIRST_NAME, LAST_NAME FROM MYTABLE WHERE 
  STATUS = 1 AND (
    VARIABLE = 'AR' OR (SPECIAL = 1 AND VARIABLE = 'PR')
  );

SQL 对逻辑运算符有非常强大的支持,您不仅限于 AND。

SELECT FIRST_NAME, LAST_NAME FROM MYTABLE WHERE 
  STATUS = 1 AND (
    VARIABLE = 'AR' OR (SPECIAL = 1 AND VARIABLE = 'PR')
  );

SQL has very powerful support for logical operators, you aren't limited to just AND.

离旧人 2024-09-14 22:08:55

如果我理解正确并且您想要 ("AR") 或 ("PR" 和 SPECIAL = "1"),那么请尝试以下操作:

SELECT FIRST_NAME, LAST_NAME FROM MYTABLE 
WHERE STATUS = 1 AND (VARIABLE = 'AR' OR (VARIABLE = 'PR' AND SPECIAL = 1));

If I'm understanding right and you want either ("AR") or ("PR" and SPECIAL = "1"), then try this:

SELECT FIRST_NAME, LAST_NAME FROM MYTABLE 
WHERE STATUS = 1 AND (VARIABLE = 'AR' OR (VARIABLE = 'PR' AND SPECIAL = 1));
坚持沉默 2024-09-14 22:08:55

你需要 and OR 语句,试试这个:

SELECT FIRST_NAME, LAST_NAME FROM MYTABLE WHERE STATUS = 1 AND ( VARIABLE = 'PR' AND SPECIAL = 1 OR VARIABLE += 'AR' );

You need and OR statement, try this:

SELECT FIRST_NAME, LAST_NAME FROM MYTABLE WHERE STATUS = 1 AND ( VARIABLE = 'PR' AND SPECIAL = 1 OR VARIABLE += 'AR' );
撩心不撩汉 2024-09-14 22:08:55

我认为要使其按照您想要的方式工作,您需要做的就是将不同的标准分解为用括号括起来的单独的逻辑分组。如果我正确理解你的问题,你想要这样的东西:

SELECT FIRST_NAME, LAST_NAME FROM MYTABLE WHERE (STATUS = 1) AND ((VARIABLE = 'PR' AND SPECIAL = 1) OR (VARIABLE ='AR')) ;

I think all you need to do to get this to work the way you desire is to break up the different criteria into separate logical groupings surrounded by parenthesis. If I understand your question correctly, you want something like this:

SELECT FIRST_NAME, LAST_NAME FROM MYTABLE WHERE (STATUS = 1) AND ((VARIABLE = 'PR' AND SPECIAL = 1) OR (VARIABLE ='AR')) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文