Note that there are some explanatory texts on larger screens.

plurals
  1. POget max value in php (instead of mysql)
    primarykey
    data
    text
    <p>I have two msyql tables, Badges and Events. I use a join to find all the events and return the badge info for that event (title &amp; description) using the following code:</p> <blockquote> <p>SELECT COUNT(Badges.badge_ID) AS badge_count,title,Badges.description FROM <code>Badges</code> JOIN <code>Events</code> ON Badges.badge_id=Events.badge_id GROUP BY title ASC</p> </blockquote> <p>In addition to the counts, I need to know the value of the event with the most entries. I thought I'd do this in php with the max() function, but I had trouble getting that to work correctly. So, I decided I could get the same result by modifying the above query by using "ORDER BY badgecount DESC LIMIT 1," which returns an array of a single element, whose value is the highest count total of all the events.</p> <p>While this solution works well for me, I'm curious if it is taking more resources to make 2 calls to the server (b/c I'm now using two queries) instead of working it out in php. If I did do it in php, how could I get the max value of a particular item in an associative array (it would be nice to be able to return the key and the value, if possible)?</p> <p>EDIT: OK, it's amazing what a few hours of rest will do for the mind. I opened up my code this morning, and made a simple modification to the code, which worked out for me. I simply created a variable on the count field and, if the new one was greater than the old one, changed it to the new value (see the "if" statement in the following code):</p> <blockquote> <p>if ( $c > $highestCount ) { $highestCount = $c; }</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload