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.

The Database Forum


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



Closed Thread
Old 02-18-2009, 06:08 AM MySQL Group by desc
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
PHP Code:
select u.id,u.username,u.last_onlineu.adminu.countryuv.locationu.favoritegame from `users` as `uleft join `users_visits` as `uvon u.id uv.user where u.last_online date_sub(NOW(), interval $onlineintervalgroup by u.id order by uv.id descu.last_online desc 
I need user_visits to be desc (by id), any advise?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
View Public Profile Visit Galaxian's homepage!
 
 
Register now for full access!
Old 02-18-2009, 07:23 AM Re: MySQL Group by desc
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
You mean you need to have the rows from the table users ordered in a descending order by the counted number of visits that correspond to the numbers of rows into the table users_visits ?

If yes, this is totally not tested but might work:
Code:
select u.id,u.username,u.last_online, u.admin, u.country, uv.location, u.favoritegame 
from `users` as `u` 
  cross join (
    select count(*) as cnt
    from users_visits uv
    where uv.user=u.id
  ) as x
where u.last_online > date_sub(NOW(), interval $onlineinterval) 
group by u.id,u.username,u.last_online, u.admin, u.country, uv.location, u.favoritegame 
order by x.cnt desc, u.last_online desc
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 02-18-2009 at 07:26 AM..
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 02-18-2009, 08:34 AM Re: MySQL Group by desc
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
i need the group by to return 1 result in descending order. It seems to be returning ascending only.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
View Public Profile Visit Galaxian's homepage!
 
Old 02-18-2009, 08:42 AM Re: MySQL Group by desc
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
group by and order are 2 different things.
A group by cannot give something in any order.
First, you get the rows you need (with a group by if needed), then you order those rows.

So, forgetting about the order, does your query gives you back the rows you want?
If not, it's the query that need works, not the order, and maybe not the group by neither.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Old 02-19-2009, 04:03 PM Re: MySQL Group by desc
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
What I'm trying to get at is group by obtains the first result in an ascending order, and returns only that in the group. No matter which order by.

If it were possible, group by desc would be what I'd be after.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
View Public Profile Visit Galaxian's homepage!
 
Old 02-19-2009, 04:38 PM Re: MySQL Group by desc
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I'm afraid you didn't understood me.
Without worrying about the order of the rows, does the query up there gives you the good rows or not ?

If it's just a matter of ordering, then it's nothing.
If you don't have the right rows, than there is something else to change in the query.

Again, a "group by" clause does not order anything.
You cannot order something with a "group by", you can only limit the number of rows you are getting back on a specific criteria.

So, if the rows are not those you expect, then the "group by" criteria is wrong.
If the rows are correct, then the "order by" clause need some work.
Maybe a sub query is needed to filter the group by result, I don't know.
It's up to you to tell me.

By the way, the table definitions and some sample datas would be of great help.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
View Public Profile Visit tripy's homepage!
 
Closed Thread     « Reply to MySQL Group by desc
 

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.26469 seconds with 12 queries