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

时间:2015-01-16 02:22:28

标签: php mysql

我有一个SQL语句让我得到像这样的结果表......

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列中至少有4个不同ID的行,这些ID共享相同的categoryID,然后按categoryID对它们进行分组。例如,将上表转换为......

categoryID | subCategoryID | categoryName
-------------------------------------------
1          | 2             |  Animals & Pets
4          | 0             |  Books & Magazines
8          | 0             |  Computer

到目前为止我的SQL是..

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

我尝试过使用...

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的类别。任何想法?

2 个答案:

答案 0 :(得分:1)

使用Having子句过滤具有至少4个不同categoryID的{​​{1}}。试试这个。

subCategoryID

但我不确定你select categoryID , categoryName from yourtable group by categoryID , categoryName having count(distinct subCategoryID)>= 4 的预期输出。

如果您希望至少每categoryID=4 subCategoryID category可能不是distinct,那么请使用此功能。

select categoryID , categoryName
from yourtable
group by categoryID , categoryName
having count(subCategoryID)>= 4

答案 1 :(得分:0)

如何加入临时表?

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 );