查询帮助 - Where子句

时间:2013-07-06 20:03:46

标签: php sql

然后我有以下查询:

mysql_select_db($database_conndb1, $conndb1);
$query_rsName = sprintf("
SELECT DISTINCT 
table1.search_id, table1.search1, table1.search2, table1.search3, table1.search4, table1.search5, table1.search6, table1.search7, table1.search8, table1.search9, table1.search10, table1.search11, table1.search12, table1.search13, table1.search14, table1.search15, table1.search16, table2.search_id 

FROM table1, table2 
WHERE
 table2.criteria1 = %s OR
 table2.criteria2 = %s OR
 table2.criteria3 = %s OR
 table2.criteria4 = %s OR
 table2.criteria5 = %s OR
 table2.criteria6 = %s OR
 table2.criteria7 = %s OR
 FIND_IN_SET(%s, table2.criteria8) OR 
 table2.criteria9 = %s OR 
 table2.criteria10 = %s OR 
 table2.criteria11 = %s AND 
 table1.search_id = table2.search_id 
ORDER BY
 table1.search2 DESC", 
 GetSQLValueString($search1_rsName, "text"),
 GetSQLValueString($search2_rsName, "text"),
 GetSQLValueString($search3_rsName, "text"),
 GetSQLValueString($search4_rsName, "text"),
 GetSQLValueString($search5_rsName, "text"),
 GetSQLValueString($search6_rsName, "text"),
 GetSQLValueString($search7_rsName, "text"),
 GetSQLValueString($search8_rsName, "text"),
 GetSQLValueString($search9_rsName, "text"),
 GetSQLValueString($search10_rsName, "text"),
 GetSQLValueString($search11_rsName, "text"));

但是,当运行查询时,它会提取所有记录并执行十次记录 - 而不仅仅是基于搜索条件的记录。现在,如果我将OR更改为AND并选择全部11,它就可以正常工作。因此,它与OR操作数有关。但是,我无法弄清楚出了什么问题。除了OR之外我还可以使用什么操作数来实现 - 允许搜索者选择1,2,3或更多标准?

2 个答案:

答案 0 :(得分:0)

这是一个查询的地狱,我猜它会运行多年。不过,这里有一个更正:

mysql_select_db($database_conndb1, $conndb1);
$query_rsName = sprintf("
SELECT DISTINCT 
table1.search_id, table1.search1, table1.search2, table1.search3, table1.search4, table1.search5, table1.search6, table1.search7, table1.search8, table1.search9, table1.search10, table1.search11, table1.search12, table1.search13, table1.search14, table1.search15, table1.search16, table2.search_id 

FROM table1, table2 
WHERE (
 table2.criteria1 = %s OR
 table2.criteria2 = %s OR
 table2.criteria3 = %s OR
 table2.criteria4 = %s OR
 table2.criteria5 = %s OR
 table2.criteria6 = %s OR
 table2.criteria7 = %s OR
 FIND_IN_SET(%s, table2.criteria8) OR 
 table2.criteria9 = %s OR 
 table2.criteria10 = %s OR 
 table2.criteria11 = %s) AND 
 table1.search_id = table2.search_id 
ORDER BY
 table1.search2 DESC", 
 GetSQLValueString($search1_rsName, "text"),
 GetSQLValueString($search2_rsName, "text"),
 GetSQLValueString($search3_rsName, "text"),
 GetSQLValueString($search4_rsName, "text"),
 GetSQLValueString($search5_rsName, "text"),
 GetSQLValueString($search6_rsName, "text"),
 GetSQLValueString($search7_rsName, "text"),
 GetSQLValueString($search8_rsName, "text"),
 GetSQLValueString($search9_rsName, "text"),
 GetSQLValueString($search10_rsName, "text"),
 GetSQLValueString($search11_rsName, "text"));

注意ORWHERE部分的括号。

答案 1 :(得分:0)

SELECT DISTINCT 
table1.search_id, table1.search1, table1.search2, table1.search3, table1.search4, table1.search5, table1.search6, table1.search7, table1.search8, table1.search9, table1.search10, table1.search11, table1.search12, table1.search13, table1.search14, table1.search15, table1.search16, table2.search_id 

FROM table1 
join table2 on table1.search_id = table2.search_id 
WHERE
 table2.criteria1 = %s OR
 table2.criteria2 = %s OR
 table2.criteria3 = %s OR
 table2.criteria4 = %s OR
 table2.criteria5 = %s OR
 table2.criteria6 = %s OR
 table2.criteria7 = %s OR
 FIND_IN_SET(%s, table2.criteria8) OR 
 table2.criteria9 = %s OR 
 table2.criteria10 = %s OR 
 table2.criteria11 = %s 
ORDER BY
 table1.search2 DESC"
相关问题