2010-07-21 6 views
0

SQLite에서 매우 복잡한 쿼리가 발생했습니다. 어떻게해야하는지 이해하는 데 도움이 필요합니다. 나는 기본적으로 검색 문자열에 .. (즉, 와일드 카드 "%의 XYZ의 %") 내가 원하는 쿼리를 수행 할 지금SQLite Complex Query Help

Category: 

CatID | CatTitle 
---------------- 
    1 | XYZ 
    2 | Sample 


Content: 

ItemID | ItemCatID | ItemText | ItemText2 | ItemText3 | ItemText4 
----------------------------------------------------------------- 
    1 |  1  | Test | Bla | Sample | MoreContent 
    2 |  1  | Test2 | BlaBla | Sample2 | Other Content 
    3 |  2  | Test3 | BlaBla2 | Sample3 | Other Content2 

:

다음의 예는 내 데이터베이스입니다 CatTitle뿐 아니라 ItemText, ItemText2 및 ItemText3도 검색하십시오.

CatID, CatTitle, ItemID 및 가능한 경우 "ItemFilteredText"(ItemText에서 ItemText4까지 모두 가능) 어느 쪽이 경기인가)

Heres the 쿼리가 CatTitle과 일치하는 경우이 항목 ID를 반환하는 경우 일, 다음, 그것은

select DISTINCT Category.CatID, 
     Category.CatTitle, 
     Content.ItemID, 
     Content.ItemText, 
     Content.ItemText2, 
     Content.ItemText3 
from Content,Category 
where Category.CatID = Content.ItemCatID 
     AND (Category.CatTitle like'%XYZ%' 
      OR Content.ItemText like '%XYZ%' 
      OR Content.ItemText2 like '%XYZ%' 
      OR Content.ItemText3 like '%XYZ%') 
GROUP BY Category.CatTitle ORDER BY Category.CatID ASC 

그것은 데이터를 반환 ... 마지막 항목 ID

나는 다음과 같은 SQL이 어느 정도 작업 한 첫 번째 항목 ID 및 NOT해야한다 ... 그러나 Category.CatTitle은 Content.ItemID가 1 대신에 2를 반환 함을 의미합니다.

모든 아이디어?

답변

0

작업을 수행해야합니다 (Content.ItemID를) 분을 포함하도록 쿼리를 수정 content.ItemID에 대한 정수를 사용하고 첫 번째 결과가 낮은 항목 ID에 의해 특징되어

select DISTINCT Category.CatID, Category.CatTitle, min(Content.ItemID), Content.ItemText, Content.ItemText2, Content.ItemText3 from Content,Category where Category.CatID = Content.ItemCatID AND (Category.CatTitle like'%XYZ%' OR Content.ItemText like '%XYZ%' OR Content.ItemText2 like '%XYZ%' OR Content.ItemText3 like '%XYZ%') GROUP BY Category.CatTitle ORDER BY Category.CatID ASC 

가정.

+0

감사합니다. 다음 질문은 일치하는 경우 단일 별칭 아래에 ItemText3의 ItemText 값을 반환하는 방법입니다 (예 : ItemFilteredText) ... 그 외 ... CatTitle이 일치하면 CatID & CatTitle (다른 모든 값은 null이어야 함) 그렇지 않으면 ItemText에서 ItemText3까지의 항목이 일치하는 경우 해당 항목을 별칭으로 반환합니다 (단, 같은 행의 결과는 여러 개 반환하지 않습니다.) 그래서 다음과 같이 표시되어야합니다. – AAA