首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >选择字段值至少出现在其他4行中的行

选择字段值至少出现在其他4行中的行
EN

Stack Overflow用户
提问于 2015-01-16 02:22:28
回答 2查看 51关注 0票数 1

我有一条SQL语句可以让我找到这样的结果表.

代码语言:javascript
复制
categoryID | subCategoryID | categoryName
-------------------------------------------
1          | 2             |  Animals & Pets
1          | 7             |  Animals & Pets
1          | 10            |  Animals & Pets
1          | 11            |  Animals & Pets
4          | 0             |  Books & Magazines
4          | 0             |  Books & Magazines
4          | 0             |  Books & Magazines
4          | 0             |  Books & Magazines
4          | 31            |  Books & Magazines
4          | 32            |  Books & Magazines
4          | 33            |  Books & Magazines
5          | 0             |  Chemist
6          | 0             |  Cloths & Accessories
6          | 0             |  Cloths & Accessories
6          | 656           |  Cloths & Accessories
7          | 0             |  Collectables
7          | 0             |  Collectables
7          | 0             |  Collectables
8          | 0             |  Computer
8          | 0             |  Computer
8          | 0             |  Computer
8          | 0             |  Computer
8          | 0             |  Computer
8          | 0             |  Computer
8          | 56            |  Computer
8          | 60            |  Computer
8          | 61            |  Computer

现在,我只想获得在共享同一个subCategoryID的subCategoryID列中至少有4个不同ID的行,然后按categoryID对它们进行分组。比如把上面的表格变成..。

代码语言:javascript
复制
categoryID | subCategoryID | categoryName
-------------------------------------------
1          | 2             |  Animals & Pets
4          | 0             |  Books & Magazines
8          | 0             |  Computer

到目前为止我的SQL是..。

代码语言:javascript
复制
SELECT
    listing.categoryID,
    listing.subCategoryID,
    categoryName
FROM listing
LEFT JOIN productInfo USING (listingID)
LEFT JOIN sectionCategory USING (categoryID)
WHERE listing.categoryID > 0
AND listing.listingStatus = 'A'
AND listing.pauseReason = 'A'
AND productInfo.quantity > 0
ORDER BY categoryID, subCategoryID

我试过用..。

代码语言:javascript
复制
SELECT
    listing.categoryID,
    categoryName
FROM listing
LEFT JOIN productInfo USING (listingID)
LEFT JOIN sectionCategory USING (categoryID)
WHERE listing.categoryID > 0
AND listing.listingStatus = 'A'
AND listing.pauseReason = 'A'
AND productInfo.quantity > 0
GROUP BY listing.categoryID
HAVING count(*) >= 4
ORDER BY RAND()
LIMIT 6

但它似乎只删除了上市数少于4的类别。有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-16 02:26:27

使用Having子句筛选至少有4个不同subCategoryIDcategoryID。尝尝这个。

代码语言:javascript
复制
select categoryID , categoryName
from yourtable
group by categoryID , categoryName
having count(distinct subCategoryID)>= 4

但不确定您如何在预期的输出中categoryID=4

如果您希望每个category至少有4个category可能不是distinct,那么请使用此方法。

代码语言:javascript
复制
select categoryID , categoryName
from yourtable
group by categoryID , categoryName
having count(subCategoryID)>= 4
票数 1
EN

Stack Overflow用户

发布于 2015-01-16 02:37:13

加入临时桌怎么样?

DROP TEMPORARY TABLE IF EXISTS categoryIDs; CREATE TEMPORARY TABLE IF NOT EXISTS categoryIDs as( select t.categoryID from (select categoryID from table where distinct(categoryID) >=4 group by categoryID)t );

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27976360

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档