在我的表格中,我有3列SubmissionId, FieldName, FieldValue
。
我有一个问题:
SELECT SubmissionId, FieldName, FieldValue
FROM myTable
WHERE (FieldValue = "Soccer")
OR (FieldValue = "Main Hall")
OR (FieldValue = "August 23, 2017")
返回以下内容:
1508 | ClassLocation | Main Hall
1509 | EventName | Soccer
1512 | EventName | Soccer
1516 | ClassLocation | Main Hall
1520 | EventName | Soccer
1520 | EventDate | August 23, 2017
1520 | ClassLocation | Main Hall
1523 | EventName | Soccer
1524 | EventName | Soccer
1525 | ClassLocation | Main Hall
1538 | EventName | Soccer
1539 | EventName | Soccer
1539 | EventDate | August 23, 2017
1539 | ClassLocation | Main Hall
1540 | ClassLocation | Main Hall
1550 | ClassLocation | Main Hall
我现在正在尝试找出添加到此查询的最佳方法,以便我只能从SubmissionId列中获取三次重复自己的字段值,在这种情况下,它将是提交ID,是1520年和1539年。在此先感谢您的帮助,我已经有一段时间难过了。
答案 0 :(得分:1)
尝试:
SELECT SubmissionId, COUNT(SubmissionId)
FROM YourTable
GROUP BY SubmissionId
HAVING COUNT(SubmissionId) = 3
答案 1 :(得分:1)
SELECT SubmissionId
FROM myTable
WHERE FieldValue in ('Soccer','Main Hall', 'August 23, 2017')
GROUP BY SubmissionID
HAVING count(Distinct fieldName) = 3
我使用不同的incase fieldValue可以重复给定的submissionID。
你也可以绑定FieldName,以确保配对的映射
WHERE (FieldName, FieldValue) in (('EventName','Soccer'),('ClassLocation','Main Hall'),('EventDate','August 23, 2017'))
答案 2 :(得分:0)
只需将当前查询用作聚合的子查询
SELECT SubmissionId
FROM ( SELECT SubmissionId, FieldName, FieldValue
FROM myTable
WHERE FieldValue IN ( "Soccer", "Main Hall", "August 23, 2017")
) as T
GROUP BY SubmissionId
HAVING COUNT(*) = 3