Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Little help with this query?
Old 02-16-2012, 01:46 PM Little help with this query?
miki86's Avatar
Extreme Talker

Posts: 239
Location: print_r($serbia);
Trades: 0
Hi,

im stuck here and i don't know how to fix it.

I have a db table which has users ID, user grade and date when someone has voted for that user (3 fields).

Im trying to read the user that has the highest average grade for todays date, limited to one.
But the problem is that i want to read only users that have 5 or more votes.

My query looks like this, but im getting an error:

$query = "SELECT idusers, AVG(votes) AS Grade FROM rank WHERE (data = '{$dbDate}') AND ((SELECT count(ID) + 1 FROM rank) AS tmpcount WHERE tmpcount>4) GROUP BY idusers ORDER BY Grade DESC LIMIT 1";

Red part is where the problem is.
Without the >4 clause this query is working ok, but i need to count the ID's.

Any help?
************************
EDIT:
Nevermind solved it:
$query = "SELECT idusers, AVG(votes) AS Grade FROM rank WHERE (data = '{$dbDate}') GROUP BY idusers HAVING COUNT(ID) > 4 ORDER BY Grade DESC LIMIT 1";

Last edited by miki86; 02-16-2012 at 02:08 PM..
miki86 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-17-2012, 10:49 PM Re: Little help with this query?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,960
Name: Keith Marshall
Location: Connecticut
Trades: 0
How about:

Code:
SELECT
    idusers,
    AVG(votes) AS Grade
FROM
    rank
WHERE 
    data = '{$dbDate}' AND
    (
        SELECT
            count(ID)
        FROM
            rank
    ) >= 5
GROUP BY
    idusers
ORDER BY
    Grade DESC
LIMIT
    1
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 02-18-2012, 08:33 AM Re: Little help with this query?
miki86's Avatar
Extreme Talker

Posts: 239
Location: print_r($serbia);
Trades: 0
Yes, that's another way to do it.
I tried that at first but i missed the syntax somewhere because im not very familiar with advanced sql statements.
miki86 is offline
Reply With Quote
View Public Profile
 
Old 05-23-2012, 08:29 AM Re: Little help with this query?
miki86's Avatar
Extreme Talker

Posts: 239
Location: print_r($serbia);
Trades: 0
Hey
I need a little help again.
I have a table that looks like this:
Code:
###################################
# ID # Type # Info  # Expires #
###################################
# 1 # Data1 # Info1 # timestamp #
# 2 # Data1 # Info2 # timestamp #
# 3 # Data1 # Info3 # timestamp #
# 4 # Data2 # Info4 # timestamp #
# 5 # Data3 # Info5 # timestamp #
Im trying to group by "Type" and order by "ID" and "Expires" descending.
But everytime i get first row as a result which has ID = 1 instead of third row.

PHP Code:
$stamp time();
query "SELECT * FROM table1 WHERE Expires >= {$stamp} GROUP BY Type Order BY ID DESC, Expires DESC"
This gives me 1st, 4th and 5th rows.
timestamp in third row is greater than the first row.

Any suggestions?
miki86 is offline
Reply With Quote
View Public Profile
 
Old 05-23-2012, 07:03 PM Re: Little help with this query?
King Spam Talker

Posts: 1,092
Name: Paul W
Trades: 0
What purpose is the "group by type" supposed to be achieving? Is there more to the query than you're decsribing?
__________________
Great music:
Please login or register to view this content. Registration is FREE



Please login or register to view this content. Registration is FREE
PaulW is online now
Reply With Quote
View Public Profile
 
Old 05-24-2012, 04:38 AM Re: Little help with this query?
miki86's Avatar
Extreme Talker

Posts: 239
Location: print_r($serbia);
Trades: 0
Im trying to get the last inserted rows of every type, 3rd, 4th and 5th row in this case.
I guess grouping results isn't what i need.

With 3 separate queries:
Code:
SELECT * FROM table1 WHERE Type = 'Data1' ORDER BY ID DESC LIMIT 1;
SELECT * FROM table1 WHERE Type = 'Data2' ORDER BY ID DESC LIMIT 1;
SELECT * FROM table1 WHERE Type = 'Data3' ORDER BY ID DESC LIMIT 1;
I don't really know how many types i'll have so i need to get an array in one query.

SOLVED:
Code:
SELECT * FROM (SELECT * FROM table1 ORDER BY ID DESC) t GROUP BY Type;

Last edited by miki86; 05-24-2012 at 05:02 AM..
miki86 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Little help with this query?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19662 seconds with 11 queries