2012-08-28 3 views
0

내가이 쿼리를 얻기 위해 노력하고 바로올바른 구문은

foreach(explode(" ", trim($words)) as $word) $where[] = "LIKE '%$word%'"; 
$wheresql = implode(" OR ", $where); 
$q = "SELECT item_id, name, price, details, img FROM items WHERE (details $wheresql) OR (name $wheresql) OR (description $wheresql)"; 
$rows = $this->dba->rawSelect($q); 

쿼리는 내가 경우 지금

SELECT item_id, name, price, details, img 
FROM items 
WHERE (details LIKE '%someword%' OR LIKE '%someword%') 
    OR (name LIKE '%someword%' OR LIKE '%someword%') 
    OR (description LIKE '%someword%' OR LIKE '%someword%') 

나는 확실하지 않다 다음과 같습니다 LIKE에 대한 열을 지정하거나 다른 작업을 수행해야합니다.

감사합니다. Richard

답변

1
foreach(explode(" ", trim($words)) as $word) $where[] = "LIKE '%$word%'"; 
$q = "SELECT item_id, name, price, details, img FROM items WHERE (details ".implode(" OR details ",$where).") OR (name ".implode(" OR names ",$where).") OR (description ".implode(" OR description ",$where).")"; 
$rows = $this->dba->rawSelect($q); 
+0

감사합니다. 나는 컬럼 이름을 가진 외부 루프를 실행하고 컬럼 이름을 "wheresql"에 넣는 방식을 조금 다르게했습니다. – Richard

+0

@ 리차드 글쎄 내 SQL 문장의 좋은 점은 하나의'$ where'를 사용하여 원하는만큼의 열에서 사용할 수 있다는 것입니다. – Passerby

1

모든 LIKE에 대해 열을 지정해야한다고 생각합니다. BTW, 왜 그냥 SQL을 실행 해보십시오? 그것은 모든 것을 알려줄 것입니다.

+0

난 단지 근처에 오류가 발생합니다.별로 도움이되지 않습니다. 간단한 질문 이었지만, 도움이되었습니다. 보통 힘든 일들을 스스로 찾아 냈습니다. 나중에 훨씬 더 간단 할 수 있습니다. – Richard

관련 문제