计算重复条目并根据类别对它们进行排序

发布于 2024-12-21 18:24:21 字数 804 浏览 1 评论 0原文

只需要一些帮助来了解如何使用 SQL 语句来检查名为interest_desc 的列中的重复条目,然后根据它们分配到的类别 ID 对它们进行排序。

完美的结果将是和 echo 一样;

“钓鱼”是类别“4”中最受欢迎的兴趣,

努力知道从哪里开始:S

更新 - 感谢您的帮助,我现在有了这个,但我相信我的语法是错误的?

$interestCatPopular = "SELECT interest_desc, categoryID, MAX(num_in_cat) AS num_in_cat    FROM
(
SELECT 
interest_desc,
categoryID,
COUNT(categoryID) AS num_in_cat
FROM tbl
GROUP BY interest_desc, categoryID
) subsel 
GROUP BY interest_desc, categoryID";

$mostPopularInterest = mysql_query($interestCatPopular) or die(mysql_error());

$arrayResults = mysql_fetch_assoc($mostPopularInterest);
foreach ($arrayResults as $result) {
echo "{$result['interest_desc']} was the most popular in category   {$result['categoryID']} with {$result['num_in_cat']} occurrences\n";
}

:)

Just need some help on how to go about using a SQL statement that checks for duplicate entries in a column called interest_desc and then sorts them by what categoryID they are assigned to.

The perfect outcome would be and echo like;

"Fishing" is the most popular interest in the category "4"

Struggling to know where to begin :S

Update - I have this now thanks to your help however i believe my syntax is wrong?

$interestCatPopular = "SELECT interest_desc, categoryID, MAX(num_in_cat) AS num_in_cat    FROM
(
SELECT 
interest_desc,
categoryID,
COUNT(categoryID) AS num_in_cat
FROM tbl
GROUP BY interest_desc, categoryID
) subsel 
GROUP BY interest_desc, categoryID";

$mostPopularInterest = mysql_query($interestCatPopular) or die(mysql_error());

$arrayResults = mysql_fetch_assoc($mostPopularInterest);
foreach ($arrayResults as $result) {
echo "{$result['interest_desc']} was the most popular in category   {$result['categoryID']} with {$result['num_in_cat']} occurrences\n";
}

:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万劫不复 2024-12-28 18:24:21
SELECT a.* 
FROM 
(
   SELECT category, interest_desc, COUNT(*) as num
   FROM category_table
   GROUP BY category,interest_desc
)a
INNER JOIN 
(
 SELECT category, MAX(num) AS max_num 
 FROM 
 (
   SELECT category, interest_desc, COUNT(*) as num
   FROM category_table
   GROUP BY category,interest_desc
 )b
 GROUP BY b.category
)c ON c.category = a.category AND a.num = c.max_num
SELECT a.* 
FROM 
(
   SELECT category, interest_desc, COUNT(*) as num
   FROM category_table
   GROUP BY category,interest_desc
)a
INNER JOIN 
(
 SELECT category, MAX(num) AS max_num 
 FROM 
 (
   SELECT category, interest_desc, COUNT(*) as num
   FROM category_table
   GROUP BY category,interest_desc
 )b
 GROUP BY b.category
)c ON c.category = a.category AND a.num = c.max_num
那小子欠揍 2024-12-28 18:24:21

使用子查询,您可以首先统计每个类别的数量。由此,外部查询选择最大计数以及相应的 categoryIDinterest_desc

此查询应该为每个 categoryID 生成最流行的 interest_desc,以及它出现的次数。当然,这是未经测试的。

SELECT interest_desc, categoryID, MAX(num_in_cat) AS num_in_cat FROM
(
SELECT 
  interest_desc,
  categoryID,
  COUNT(categoryID) AS num_in_cat
FROM tbl
GROUP BY interest_desc, categoryID
) subsel 
GROUP BY interest_desc, categoryID

在 PHP 中,成功查询并将 mysql_fetch_assoc() 循环调用到数组 $results 中:

更新

您需要在循环中获取结果:

// Load $arrayResults in a loop...
$arrayResults = array();
while ($row = mysql_fetch_assoc($mostPopularInterest)) {
  $arrayResults[] = $row;
}

foreach ($arrayResults as $result) {
  echo "{$result['interest_desc']} was the most popular in category   {$result['categoryID']} with {$result['num_in_cat']} occurrences\n";
}

Using a subquery, you can first count the number in each category. From that, the outer query selects the maximum count along with the corresponding categoryID and interest_desc.

This query should result in the most popular interest_desc for each categoryID, plus how many occurrences it had. Of course, it's untested.

SELECT interest_desc, categoryID, MAX(num_in_cat) AS num_in_cat FROM
(
SELECT 
  interest_desc,
  categoryID,
  COUNT(categoryID) AS num_in_cat
FROM tbl
GROUP BY interest_desc, categoryID
) subsel 
GROUP BY interest_desc, categoryID

In PHP, following a successful query and looping call to mysql_fetch_assoc() into an array $results:

Update

You need to fetch your results in a loop:

// Load $arrayResults in a loop...
$arrayResults = array();
while ($row = mysql_fetch_assoc($mostPopularInterest)) {
  $arrayResults[] = $row;
}

foreach ($arrayResults as $result) {
  echo "{$result['interest_desc']} was the most popular in category   {$result['categoryID']} with {$result['num_in_cat']} occurrences\n";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文