Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-30-2008, 06:20 AM
jerx jerx is offline
 
Join Date: Feb 2006
Posts: 188
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default mysql: sort before group

I wanted to modify a mysql query, which displays the latest album pictures. Since many users upload many pictures at once, this would only show pictures from one user/album. Therefore I added a group query to the statement, but this does not show the latest updates any more.

This does not work:
Code:
$lpictures = $db->query_read("
  SELECT albumpicture.*, album.*, user.username, user.usergroupid,
  IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid 
  FROM " . TABLE_PREFIX . "albumpicture AS albumpicture
  LEFT JOIN " . TABLE_PREFIX . "album AS album ON(album.albumid = albumpicture.albumid)
  LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = album.userid)
  WHERE album.state = 'public' 
  GROUP BY album.userid
  ORDER BY $orderby DESC LIMIT 0, $tot  
  ");
Does anyone know how you can first sort the data and then group it?

Thank you!
Reply With Quote
  #2  
Old 10-30-2008, 10:04 PM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You can do this with a nested query

[sql]
SELECT albumpicture.*, album.*, user.username, user.usergroupid,
IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid
FROM " . TABLE_PREFIX . "albumpicture AS albumpicture
LEFT JOIN " . TABLE_PREFIX . "album AS album ON(album.albumid = albumpicture.albumid)
LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = album.userid)
WHERE album.state = 'public'
AND albumid in (select max(albumid) from album group by userid)
ORDER BY $orderby DESC LIMIT 0, $tot
[/sql]


This assume the highest albumid for a user is the latest album.

This is probably quite slow, if this is a query you are running often then I would create a new latestalbum table and set up a trigger so that it updates when the album table updates.
Reply With Quote
  #3  
Old 10-30-2008, 10:53 PM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

+1 for storing the ID you want first and joining on that
Reply With Quote
  #4  
Old 10-31-2008, 05:15 AM
jerx jerx is offline
 
Join Date: Feb 2006
Posts: 188
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you!

I think this won' t work. The album with the highest id is the latest album, but it does not necessarily hold the latest uploaded pictures of that user.

I tried your code anyway. First I ran into several error messages (table not found or subquery is ambiguous). In the end I had this:

Code:
  SELECT albumpicture.*, album.*, user.username, user.usergroupid,
  IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid 
  FROM " . TABLE_PREFIX . "albumpicture AS albumpicture
  LEFT JOIN " . TABLE_PREFIX . "album AS album ON(album.albumid = albumpicture.albumid)
  LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = album.userid)
  WHERE album.state = 'public'
  AND album.albumid in (select max(album.albumid) from vb_album group by album.userid) 
  ORDER BY $orderby DESC LIMIT 0, $tot
It does not work. It gives me pictures of the same album. The result looks like the original code, but I think if you update an old album, this will give you wrong results.

But you suggest going another route anyway. This will be shown on the index page of the domain and therefore will be running very often. Unfortunetely I have no clue how to accomplish that. I was hoping that this change would be as easy as switching from dateline sort order to random display.

I just looked at vb 3.8 beta 1. It almost has a this feature. They created a new table for the album updates, too. But it only shows the coverpicture, not the latest picture added to that album. Nonetheless I would prefer this kind of display over displaying multiple pictures of the same albums.
Reply With Quote
  #5  
Old 11-02-2008, 09:40 PM
Eikinskjaldi's Avatar
Eikinskjaldi Eikinskjaldi is offline
 
Join Date: Feb 2006
Location: Hell, never looked better
Posts: 572
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by jerx View Post
Thank you!

I think this won' t work. The album with the highest id is the latest album, but it does not necessarily hold the latest uploaded pictures of that user.

I tried your code anyway. First I ran into several error messages (table not found or subquery is ambiguous). In the end I had this:

Code:
  SELECT albumpicture.*, album.*, user.username, user.usergroupid,
  IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid 
  FROM " . TABLE_PREFIX . "albumpicture AS albumpicture
  LEFT JOIN " . TABLE_PREFIX . "album AS album ON(album.albumid = albumpicture.albumid)
  LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = album.userid)
  WHERE album.state = 'public'
  AND album.albumid in (select max(album.albumid) from vb_album group by album.userid) 
  ORDER BY $orderby DESC LIMIT 0, $tot
It does not work. It gives me pictures of the same album. The result looks like the original code, but I think if you update an old album, this will give you wrong results.

But you suggest going another route anyway. This will be shown on the index page of the domain and therefore will be running very often. Unfortunetely I have no clue how to accomplish that. I was hoping that this change would be as easy as switching from dateline sort order to random display.

I just looked at vb 3.8 beta 1. It almost has a this feature. They created a new table for the album updates, too. But it only shows the coverpicture, not the latest picture added to that album. Nonetheless I would prefer this kind of display over displaying multiple pictures of the same albums.
What field does hold the info per user you need? i.e. what query would you run to get the latest picture(s) or what ever it is you want for just one user (e.g. you)
Reply With Quote
Reply


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. The time now is 04:12 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04035 seconds
  • Memory Usage 2,212KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete