Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-31-2012, 05:41 PM   PM User | #1
muneeba9071
Regular Coder

 
Join Date: Dec 2008
Posts: 120
Thanks: 15
Thanked 2 Times in 2 Posts
muneeba9071 is an unknown quantity at this point
Question Help with a PHP-MySQL Query

Hi,

I need help to make these two queries as 1 query:

PHP Code:
$results_top_ips $wpdb->get_results("SELECT ip FROM keywords GROUP BY ip ");
foreach(
$results_top_ips as $row) {
    
$total_visits $wpdb->get_row("SELECT COUNT(*) AS total_visits FROM keywords WHERE ip = '{$row->ip}' ")->total_visits
Is it possible to put the total_visits in the first query, so then I can also do a ORDER BY total_visits DESC within the first query.

Thanks in advance!
muneeba9071 is offline   Reply With Quote
Old 10-31-2012, 06:47 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yeah, I'm not sure why you have the two in the first place. The first one is only missing the count for it; the group by has no usage without an aggregate such as count. The only thing I can think of what you meant that to do was to query for DISTINCT ip.

Code:
SELECT ip, count(*)  AS total_visits
FROM keywords
GROUP BY ip
ORDER BY total_visits DESC
I'm pretty sure MySQL lets you order by an alias, but I'd have to test it out to be sure. If not, you can order by count() again.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:49 AM.


Advertisement
Log in to turn off these ads.