How can i get a mysql common field name that have highest vivit ?

Hello i have this my sql entry result showed below what i need is an either mysql query or a php function that return to me what is the section that have most visited regardless to the country where the answer based on the result below is
featured products with a 8 visits


So how can i retrieve that result


Thanks.
id      refer                                                section                   date             visit  country             c
---------------------------------------------------------------------------------------------------------------------
2	product/159/electric-oud-eq	        featured products	03-07-2009  1	   Germany	        no
3	product/559/trsamples-korg-pa800	featured products	03-07-2009  2    Germany	        no
4	product/446/universal-bc	        featured products	03-07-2009  2    Germany	        no
5	product/559/trsamples-korg-pa800	featured products	03-07-2009  1	  United States	       no
6	product/559/trsamples-korg-pa800	featured products	03-07-2009  2	  Australia	        no
7	product/159/electric-oud-eq	        news products	04-07-2009  2	   United States	no

Open in new window

StylemindsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

boowhupCommented:
assuming your table is called table.....

This would return the entry with the most visits.
     Select top 1 * from table order by visit  ;

If you wanted the one with 8 visits only.
     Select Top 1 * from table where visit = 8 order by visit ;

If you wanted _all_ results with 8 visits.
     Select * from table where visit = 8 order by visit ;


0
jfromanskiCommented:
if your id key is unique, than
SELECT a.section,SUM(b.visit) FROM table AS a, table AS b WHERE a.id=b.id GROUP BY a.section;



0
jfromanskiCommented:
of course - this will return all sections, lets order them:
SELECT a.section,SUM(b.visit) AS s
FROM yourtable AS a, yourtable AS b
WHERE a.id=b.id
GROUP BY a.section
ORDER BY s DESC;
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

StylemindsAuthor Commented:
Sorry but didn't get what a and what b ?

Let explain more what i need to prevent any misunderstanding.


What i need a result of what  section row results  (having same name ) have higher  total of visits
where here we have two results with different name featured products and near products so if we do calculate manually the featured products have more visits where it we calculate the visits all together it gives 8


Hope this get the issue more clear so what i need is a function or mysql command that retrieve this result
0
Roger BaklundCommented:
No need for a self join in this case, just do:
select section,sum(visits) as total_visits
from TableName
group by section
order by total_visits desc
limit 1

Open in new window

0
StylemindsAuthor Commented:
ok how can i gather the section name that have higher visits and it related visits total ?
0
jfromanskiCommented:
a and b are aliases for "yourtable", s is an alias for SUM(b.visit). This query will
1. select all pairs (a.section,sum(b.visit)) where a.id=b.id
so it will be in your case
featured products 8
featured products 8
featured products 8
featured products 8
featured products 8
news products 2
2. then group this results by section, so you get
featured products 8
news products 2
3. then order this recordset descending by s (i.e. sum(visit)), giving at last
featured products 8
news products 2
0
Roger BaklundCommented:
>> how can i gather the section name that have higher visits and it related visits total ?

Did you try my suggestion?
0
StylemindsAuthor Commented:
all you gave cxr is a mysql_query so i am not sure what to do next so whoever willing to help please write a full coded solution with comment on the lign that need to be clarified that will help much better as i am knew into this thanks in advance.
0
jfromanskiCommented:
cxr: I know that there is no need for self join, but this way it's easier to explain.
0
jfromanskiCommented:
What you want to do if you will have two or more sections with equal and highest number of visits?
0
StylemindsAuthor Commented:
what do you mean can you please give example .
0
Roger BaklundCommented:
>> all you gave cxr is a mysql_query

You asked for "either mysql query or a php function"...?

Below it is as a function. Call it like this:

$section = SectionWithMostVisits();

If you want it to return both the section name and the visit count, change the last line into this:

return array($row['section'],$row['total_visits']);

...and call it like this:

list($section,$visits) = SectionWithMostVisits();
function SectionWithMostVisits() {
  $sql = "select section,sum(visits) as total_visits
from TableName
group by section
order by total_visits desc
limit 1";
  $res = mysql_query($sql);
  if(!$res) die(mysql_error());
  $row = mysql_fetch_assoc($res);
  return $row['section'];
}

Open in new window

0
StylemindsAuthor Commented:
thanks CXR so if i echo $row['visit'] will i get the total number of visits ! as i my result need to be

for example


the highest surfed is featured products section  with 8 visits
0
jfromanskiCommented:
See php code showing all results in a table.
<?php
 
$result = mysql_query("SELECT a.section,SUM(b.visit) AS s FROM yourtable AS a, yourtable AS b WHERE a.id=b.id GROUP BY a.section ORDER BY s DESC;");
 
?> <table> <?php
 
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
    echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td></tr>
}
 
?>
</table>

Open in new window

0
jfromanskiCommented:
missed double quote
<?php
 
$result = mysql_query("SELECT a.section,SUM(b.visit) AS s FROM yourtable AS a, yourtable AS b WHERE a.id=b.id GROUP BY a.section ORDER BY s DESC;");
 
?> <table> <?php
 
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
    echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td></tr>";
}
 
?>
</table>

Open in new window

0
Roger BaklundCommented:
>> if i echo $row['visit'] will i get the total number of visits

Not quite. The function (with the modification) returns an array with two items. Call it like this:

list($section,$visits) = SectionWithMostVisits();

...then the variables $section and $visits will contain the section name and the visit count, respectively.
0
StylemindsAuthor Commented:
Thanks jfromanski and cxr for your help a question for jfromanski so if i got two section that have different name but have same visits number so what i will get do i get in looping while two results ?
0
jfromanskiCommented:
This code will return all sections with highest sum of visits:
<?php
 
$result = mysql_query("SELECT a.section,SUM(b.visit) AS s FROM yourtable AS a, yourtable AS b WHERE a.id=b.id GROUP BY a.section ORDER BY s DESC;");
 
?> <table> <?php
 
$high=0;
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
    if( $row[1]>=$high ){
      echo "<tr><td>".$row[0]."</td><td>".$row[1]."</td></tr>";
      $high = $row[1];
    }
}
 
?>
</table>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StylemindsAuthor Commented:
Sorry i am giving your hard times but didn't get what you meant ! is that i gives by asc order begining by highest visits to the lowest ! or just the highest beside you don't reply my question if there section have same igher visits do they will be arrayed and listed or only one ! please explain
0
jfromanskiCommented:
All sections with the highest number of visits will be displayed. For the data as below you get:
other products      4
featured products 4      



id      refer                                                section                   date             visit  country             c
---------------------------------------------------------------------------------------------------------------------
2       product/159/electric-oud-eq             other products       03-07-2009  1      Germany              no
3       product/559/trsamples-korg-pa800        featured products       03-07-2009  2    Germany                no
4       product/446/universal-bc                featured products       03-07-2009  2    Germany                no
5       product/559/trsamples-korg-pa800        other products       03-07-2009  1     United States        no
6       product/559/trsamples-korg-pa800        other products       03-07-2009  2     Australia             no
7       product/159/electric-oud-eq             news products   04-07-2009  2      United States        no

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Security

From novice to tech pro — start learning today.