简体   繁体   English

针对多个MYSQL列查询PHP变量

[英]Query PHP variable against multiple MYSQL columns

I have a php variable called industry, what i want to do is query that variable against the mysql database. 我有一个称为行业的php变量,我想做的就是针对mysql数据库查询该变量。 However the mysql database has five columns in which that variable might be present, as indicated below. 但是,mysql数据库有五个列,其中可能存在该变量,如下所示。

industry ------> industry 1 industry 2 industry 3 industry 4 industry 5 行业------>行业1行业2行业3行业4行业5

below is my current php code. 下面是我当前的php代码。

     $query = "SELECT * FROM data where listing = 'rec' and industry='$industry'";

is there a way to query my php variable against all of the columns called industry? 有没有一种方法可以针对称为行业的所有列查询我的php变量?

I take your line "industry 1 industry 2 industry 3 industry 4 industry 5" means that there are five columns, named "industry1" through "industry5". 我将“行业1行业2行业3行业4行业5”列为一行,这意味着有五个列,分别命名为“ industry1”至“ industry5”。

In this case you can combine the checks with an or operator. 在这种情况下,您可以将支票与or运算符组合。

SELECT * FROM data where listing = 'rec' and (industry1='$industry' or
  industry2='$industry' or industry3='$industry' or
  industry4='$industry' or industry5='$industry')

You're going to have to chain together the different possibilities in your WHERE clause. 您将必须将WHERE子句中的各种可能性链接在一起。

SELECT *
FROM   DATA
WHERE  LISTING = 'REC'
       AND (    INDUSTRY_1 = '$INDUSTRY'
             OR INDUSTRY_2 = '$INDUSTRY'
             OR INDUSTRY_3 = '$INDUSTRY'
             OR INDUSTRY_4 = '$INDUSTRY'
             OR INDUSTRY_5 = '$INDUSTRY');

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

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