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"));

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

4

2 回答 2

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"
于 2013-07-06T21:25:14.023 回答
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"));

OR注意部分WHERE条款的括号。

于 2013-07-06T20:10:51.187 回答