当特定字段匹配时,在php中查找mysql查询的列名


Finding the column name in php for a mysql query when a particular field has matched

我有一个查询,我正在搜索一个字段在10列,如

price_squery1 = "select * from table 
where table_category1 = '$category[0]' or 
table_category2 = '$category[0]' or 
table_category3 = '$category[0]' or 
table_category4 = '$category[0]' or 
table_category5 = '$category[0]' or 
table_category6 = '$category[0]' or 
table_category7 = '$category[0]' or 
table_category8 = '$category[0]' or 
table_category9 = '$category[0]' or 
table_category10 = '$category[0]'";
$price_sresult1 = mysql_query($price_squery1);

我想知道字段匹配了哪一列,而不使用for循环来搜索每一列。是否有一种方法可以直接找到匹配发生的列。我是sql新手,请帮助。

我真的不知道如何在SQL中做到这一点,但是您可以比较您搜索的值,以及PHP中每列返回的值?

可能有多个列匹配值…

$result = mysql_fetch_assoc(mysql_query($price_squery1);
if ($result['table_category1'] == $category[0])
    // found in category1
if ($result['table_category2'] == $category[0])
    // found in category2
price_squery1 = "select 
if(table_category1 = '$category[0]', ''table_category1'',if(table_category2 = '$category[0]',''table_category2'',if(table_category3 = '$category[0]',''table_category3'',if(table_category4 = '$category[0]',''table_category4'', None)))) matched_category
t.* from table t
where table_category1 = '$category[0]' or 
table_category2 = '$category[0]' or 
table_category3 = '$category[0]' or 
table_category4 = '$category[0]' or 
table_category5 = '$category[0]' or 
table_category6 = '$category[0]' or 
table_category7 = '$category[0]' or 
table_category8 = '$category[0]' or 
table_category9 = '$category[0]' or 
table_category10 = '$category[0]'";
$price_sresult1 = mysql_query($price_squery1);

我修改了前几个类别的查询,您可以扩展它,添加更多的if语句。