Database SQLite复杂查询帮助
我在SQLite中有一个非常复杂的查询,我需要一些帮助来理解如何执行它 以下是我的数据库的示例:Database SQLite复杂查询帮助,database,sqlite,Database,Sqlite,我在SQLite中有一个非常复杂的查询,我需要一些帮助来理解如何执行它 以下是我的数据库的示例: Category: CatID | CatTitle ---------------- 1 | XYZ 2 | Sample Content: ItemID | ItemCatID | ItemText | ItemText2 | ItemText3 | ItemText4 ---------------------------------------------------
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
现在,我基本上想对搜索字符串进行查询。。(即带有通配符的“%XYZ%”),我不仅要搜索CatTitle,还要搜索ItemText、ItemText2和ItemText3
但是我想返回的参数是:CatID、CatTitle、ItemID,如果可能的话,还有“ItemFilteredText”(可以是ItemText到ItemText4的任何内容,具体取决于匹配的内容)
问题是,如果查询匹配CatTitle,那么如果它返回ItemID,它应该是第一个ItemID,而不是最后一个ItemID
我有以下SQL正在运行
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
它返回数据。。。但按Category.CatTitle分组意味着Content.ItemID将返回2,而不是1
有什么想法吗?假设您对content.ItemID使用整数,并且第一个结果的特征是较低的ItemID,请修改查询以包含min(content.ItemID)
应该做这项工作。谢谢,做得很好。我想下一个问题是,如果ItemText的值匹配,如何在单个别名下将其返回给ItemText3。。。(即ItemFilteredText)。。。除此之外。。。如果CatTitle匹配,则仅返回CatID和CatTitle(所有其他值应为null),否则,如果ItemText到ItemText3中的任何项匹配,则将其作为别名返回(但不返回同一行的多个结果。因此,它应显示:
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