2013-07-06 4 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")); 

을, 모든 기록을 끌어와 각 레코드에 10 번 않습니다 - 검색 기준에 따라뿐만 아니라 사람을 . 이제 OR을 AND로 변경하고 모두 11을 선택하면 정상적으로 작동합니다. 따라서 OR 피연산자와 관련이 있습니다. 그러나 나는 무엇이 잘못되었는지 알 수 없다. 검색 자에게 1, 2, 3 또는 그 이상의 기준을 선택할 수 있도록 OR 대신 어떤 피연산자를 사용할 수 있습니까?

+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의 고급 주제가 아닙니다. 실제로 그것은 필수적입니다. 중요한 것은 결코 중요한 것을 연기하는 것이 아닙니다. 당신은 그들을 밖으로 google 수 있으며 기본 조인 무엇인지 이해하십시오. 어쨌든 그 table1.search_id = table2.search_id는 비록 가입이라고 말합니다. 나는 그걸로 움직여서 OR과 AND가 그 기준에 간섭하지 않도록 해주었습니다. 현실에서는 elf r에 참여했습니다 !! 하지만 그것을 읽는 것이 낫다 .. – user1974729