简体   繁体   English

过滤 SQL 结果

[英]Filter for SQL Results

I have the following query written我写了以下查询

SELECT *
FROM 
    (((SocialAndTags 
JOIN 
    SocialManagementTracker ON SocialAndTags.SMTrackerID = SocialManagementTracker.SocialID)
JOIN 
    ProgramCategories ON SocialAndTags.ProgramCategoriesID = ProgramCategories.ProgramID)
JOIN 
    TagCategories ON SocialAndTags.TagCategoriesID = TagCategories.TagID) 
WHERE 
    InitialComment > '2020-03-16 00:00:00.999' 
    AND InitialComment < '2020-03-17 23:59:59.999'
ORDER BY 
    SMTrackerID DESC 

This executes no problem.这执行没有问题。

Here is my issue.这是我的问题。 I have 3 tables.我有3张桌子。 TagCategories tables holds the ID/Tag Name. TagCategories表包含 ID/标签名称。 SocialManagementTracker holds the Item and more details. SocialManagementTracker保存项目和更多详细信息。 SocialAndTags table has a unique ad and is pulling the TagCategories and SocialManagementTracker unique IDs. SocialAndTags表有一个唯一的广告,并且正在拉取TagCategoriesSocialManagementTracker唯一 ID。

When users are entering multiple tags for an item, its appearing the in the SocialAndTags table twice, which is how I set it up.当用户为一个项目输入多个标签时,它会在 SocialAndTags 表中出现两次,这就是我设置它的方式。 How do I then use the above query to filer out the duplicate item results?然后我如何使用上面的查询来过滤出重复的项目结果?

Or did I do this backwards and I should have added another column(s) to my SocialAndTags table?或者我是不是反过来做了,我应该在我的 SocialAndTags 表中添加另一列?

I've reviewed my database and found I was referencing the wrong foreign key.我查看了我的数据库,发现我引用了错误的外键。 I also used DISTINCT to get the results I required.我还使用了 DISTINCT 来获得我需要的结果。

SELECT 
DISTINCT
SocialID, ProgramArea, Description, SocialURL, InitialComment, DaysToResolve, ResolutionDate FROM 
SocialAndTags 
JOIN SocialManagementTracker ON SocialAndTags.SMTrackerID = SocialManagementTracker.SocialID
JOIN ProgramCategories ON SocialAndTags.ProgramCategoriesID = ProgramCategories.ProgramID
WHERE 
InitialComment > '2020-03-16 00:00:00.999' 
AND InitialComment < '2020-03-17 23:59:59.999'
ORDER BY ResolutionDate, InitialComment DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM