简体   繁体   English

sql查询以根据组合条件返回数据

[英]sql query to return data based on combination conditions

Need to code a sql to filter records form a table based on combination conditions. 需要编写一个sql以根据组合条件过滤形成表格的记录。 show records if column_1 is "A" and then column_2 is "100" OR if column_1 is "B" and then column_2 is "200" 显示记录,如果column_1是“ A”,然后column_2是“ 100”,或者如果column_1是“ B”,然后column_2是“ 200”

    select * 
    from tableTmp
    where 
name="student"
and ((column_1="A" and column_2 = "100") OR (column_1="B" and column_2 = "200"))

when the first condition is not there, it will show what we want. 当第一个条件不存在时,它将显示我们想要的。 but if there is first condition, then column_1="C" data still will show, the query is broken. 但是如果存在第一个条件,则仍然会显示column_1 =“ C”数据,查询将被破坏。

do you know why ? 你知道为什么吗 ?

Use single quotes for literals: 对文字使用单引号:

select * 
from tableTmp
where name='student'
and (
    (column_1='A' and column_2 = '100') 
 OR (column_1='B' and column_2 = '200')
 )

However I cannot see how a column_1 value of 'C' can be returned by that query. 但是我看不到该查询如何返回column_1的'C'值。

Nb. Nb。 If column_2 is a numeric data type don't use single quotes. 如果column_2是数字数据类型,请不要使用单引号。

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

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