0

I want to make a voting list kind of thing. I have a form that saves the entered data to mysql database. Suppose a user entered "Something". It'll be saved in column called "bname". Now what I want is Suppose 10 people entered "Something" or "something"(case sensitivity to be ignored automatically if there is a way?) then I want: "Something - 10" to be printed in php page.

My try:

$query="SELECT bname, count 
FROM brandnames 
GROUP BY bname 
HAVING COUNT(*) > 0
LIMIT 11";

$result=mysqli_query($con,$query) or die('Error!: ' . mysqli_error($con));

$query2="SELECT count
FROM brandnames
GROUP BY count
HAVING COUNT(*) > 0
ORDER BY count DESC
LIMIT 11";

$result2=mysqli_query($con,$query2) or die('Counting Error!: ' . mysqli_error($con));

while ($row=mysqli_fetch_assoc($result))
{
while ($row2=mysqli_fetch_assoc($result2))
{
echo ($row['bname'] . '&nbsp' . '-' . '&nbsp' . $row2['count'] . '<br />');
}
}

This thing prints Something - 4 Something - 3

I do not want to use this method. I posted this as if I don't post someone will point me out to post. :/

I don't want to use "count" column. I want a simple voting list as I mentioned above.

Thanks, :)

4

2 回答 2

1

您要查找的查询应该类似于

select bname, count(*) as nbr
  from brandnames
 group by bname
 order by nbr desc
limit 11;

count(*) 将返回按 bname 组合在一起的行数。区分大小写取决于您的联盟。如果它是区分大小写的联盟,则需要改为按 lower(bname) 分组。请注意,这可能会阻止索引为此查询工作。

于 2013-03-07T20:39:11.653 回答
0

感谢大家。我开始知道如何做到这一点。希望这对某人有所帮助:) 特别感谢@Andreas Wederbrand

<?php

//Create Connection

$query="SELECT columnname, count(*)
AS alias
FROM tablename
GROUP BY columnname
ORDER BY alias DESC";

$result=mysqli_query($con,$query) or die('Counting Error!: ' . mysqli_error($con));



while ($row=mysqli_fetch_array($result))
{
echo ($row['columnname'] . '&nbsp' . '-' . '&nbsp' . $row['alias'] . '<br />');
}

//Close Connection
?>
于 2013-03-07T21:38:50.160 回答