简体   繁体   English

SQL查询 如果column =特定值,则返回全部

[英]Sql query. Return all if column = a particular value

I have a small question concerning an SQL query I am using in a PHP project, more precisely my Where Statement. 我有一个关于在PHP项目中使用的SQL查询的小问题,更确切地说是我的Where语句。

So let's say I have a table named tab. 假设我有一个名为tab的表。 This table has 3 columns: id, name and position. 该表有3列:id,名称和位置。 name is a varchar and position is an int. 名称是varchar,位置是int。

What I am trying to do is to find all rows of the table where the values of the colums are equal to the value of variables (Lets call them $name and $pos). 我想做的是找到表的所有行,其中列的值等于变量的值(我们称它们为$ name和$ pos)。 So normaly I would simply do something like this: 所以通常我会做这样的事情:

private function getSqlQuery($name, $pos){
    return "SELECT *
            FROM tab
            WHERE name LIKE '%" . $name . "%' 
            AND position = " . $pos;
}

To clarify, the variable $pos is not strictly integer and could be a char or string and I have control over what value to send in parameter. 需要说明的是,变量$ pos不是严格的整数,可以是char或字符串,我可以控制要发送的参数值。

Now here is the catch with what I need to do: this query must be able to also return every row that respect the first condition (name = $name) must be returned, regardless of the value of position. 现在这是我需要做的事情:此查询还必须能够返回尊重必须返回第一个条件(名称= $ name)的每一行,而不管位置的值如何。

I also cannot use multiple SQL query, I won't go into detail as to why as it would take too long, but I cannot call 2 different query. 我也不能使用多个SQL查询,我不会详细说明为什么会花很长时间,但是我无法调用2个不同的查询。 I can only modify the values of the parameters ($name and $pos). 我只能修改参数的值($ name和$ pos)。

I need to find some sort of Wildcard, a value to send to $pos so it would return anything. 我需要找到某种通配符,一个要发送给$ pos的值,以便它将返回任何内容。 Using LIKE '%%' for name worked, but I have yet to find one for an int value. 使用LIKE'%%'作为名称有效,但是我还没有找到一个int值。

I apologize for my poor english. 我为我的英语不好而道歉。

I never used something like this, but in theory i think it could work: 我从来没有使用过这样的东西,但是从理论上讲,我认为它可以工作:

You could use pass a specific string in $var and check for it with a CASE in WHERE clause. 您可以在$ var中使用传递特定字符串,并在WHERE子句中使用CASE进行检查。 If it is that string you compare position with position, which gives you the results from the first validation: 如果是该字符串,则将position与position进行比较,这将为您提供第一次验证的结果:

 SELECT *
 FROM tab
 WHERE name LIKE '%" . $name . "%' 
 AND position = CASE WHEN " .$var. " = 'IGNORE' THEN position ELSE " .$var. "END;

First, I think $var should be $pos : 首先,我认为$var应该是$pos

        SELECT *
        FROM tab
        WHERE name LIKE '%" . $name . "%' AND
              position = " . $pos;

A typical condition would be if you passed in a special value for pos, then you would return all rows that match name. 一个典型的条件是,如果您为pos传递了一个特殊值,那么您将返回所有与name匹配的行。 Say that pos must always be non-negative, then you could use -1: 假设pos必须始终为非负数,则可以使用-1:

        SELECT *
        FROM tab
        WHERE name LIKE '%" . $name . "%' AND
              (position = " . $pos . " or " . $pos . " = -1)";

The only question is what out-of-range value you can use for the comparison. 唯一的问题是可以用于比较的超出范围的值。 If there are none, you could change $pos to a string and then use the empty string for this purpose. 如果没有,则可以将$pos更改$pos字符串,然后将空字符串用于此目的。

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

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