2013-07-06 149 views
0

然后我有以下查询:查询帮助 - Where子句

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或更多条件?

+0

请格式化您的代码/查询。并请狐狸你的标题(使其描述)。 – PeeHaa

+0

如果您看到由X返回的相同记录加入表中的行数,那么您将获得笛卡尔联接... – Trent

回答

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

注意括号WHERE条款左右OR一部分。

+0

谢谢,谢谢,谢谢!我知道这是一种更古老的编码方式 - 但这是我所了解和理解的。我用圆括号尝试了很多东西,但并不像你所建议的那样 - 这很棒! –

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" 
+0

我还不了解JOIN语句,但一直在努力学习,因为我在研究我的问题时一直在跑过它们。感谢您的回答,我会牢记在心。 –

+0

我建议你应该先做一些关于连接的阅读。它不是SQL中的高级主题。其实这是一个必不可少的。重要的是不要推迟重要的事情。你可以谷歌他们了解什么是基本的加入。无论如何说table1.search_id = table2.search_id是一个连接虽然。我刚刚把它移动到确保OR和AND不会干扰这个标准。你在现实中加入了自己!但最好把它读出来.. – user1974729