0

我有一个名为 site_attributes 的表,它看起来类似于:

siteId     attributeId     tier
-------------------------------
site1id    attribute1id    1
site1id    attribute2id    1
site1id    attribute3id    1

如何从表中选择attributeId 等于attribute1id 且attributeId 等于attribute3id 的唯一站点ID?

4

1 回答 1

2
SELECT siteId, count(*) ct
FROM TableName
WHERE attributeId IN ('attribute1id', 'attribute3id')
GROUP BY siteId
HAVING ct = 2

或者

SELECT t1.siteId
FROM TableName t1
JOIN TableName t2 USING (siteId)
WHERE t1.attributeId = 'attribute1id'
AND t2.attributeId = 'attribute3id'
于 2012-11-03T16:39:40.857 回答