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 03-06-2007, 10:02 PM
Mistah Roth's Avatar
Mistah Roth Mistah Roth is offline
 
Join Date: Jul 2003
Location: Ottawa, Ontario
Posts: 70
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default SELECT DISTINCT problems

My script displays the last 5 threads, a user has posted in. The main query is as follows:

SELECT distinct threadid FROM ". TABLE_PREFIX . "post WHERE userid = " . $vbulletin->userinfo['userid'] . " ORDER BY postid DESC LIMIT 5

That should work... because if you remove DISTINCT it shows the last posts you made including duplicates of the same thread if you posted more than once in a thread.

For some reason, when you add DISTINCT, it does not return the last 5 threads posted in. For some reason it skips any older threadids. If I remove the distinct from the query, it shows the last 5 posts properly. The problem is we can't have repeats, so DISTINCT is supposed to solve that.

Anyone have any idea whats going wrong? If all else fails I'll just make it so that it filters out doubles with PHP code.... not as efficient as I'd like it though, there should be no reason the query doesnt work.
Reply With Quote
  #2  
Old 03-06-2007, 11:40 PM
Zachariah's Avatar
Zachariah Zachariah is offline
 
Join Date: Feb 2002
Location: Canoga Park, CA
Posts: 2,125
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Code:
SELECT distinct threadid FROM ". TABLE_PREFIX . "post WHERE userid = '" . $vbulletin->userinfo['userid'] . "' ORDER BY postid DESC LIMIT 5
I would say you need to add single quotes around " . $vbulletin->userinfo['userid'] . "
Reply With Quote
  #3  
Old 03-07-2007, 06:40 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Zachariah View Post
Code:
SELECT distinct threadid FROM ". TABLE_PREFIX . "post WHERE userid = '" . $vbulletin->userinfo['userid'] . "' ORDER BY postid DESC LIMIT 5
I would say you need to add single quotes around " . $vbulletin->userinfo['userid'] . "
It is a numeric column, so no quotes needed.

[sql]SELECT DISTINCT threadid FROM post WHERE userid = 1 ORDER BY dateline DESC LIMIT 5[/sql]

This seems to work correct. But might need to test more with different test data.

What MySQL version are you using?
Reply With Quote
  #4  
Old 03-08-2007, 04:26 AM
Mistah Roth's Avatar
Mistah Roth Mistah Roth is offline
 
Join Date: Jul 2003
Location: Ottawa, Ontario
Posts: 70
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

MySQL version 4.1.18

If I run the following Query, here are my results (I increase the limit number to show)

SELECT distinct threadid FROM post WHERE userid = 1 ORDER BY postid DESC LIMIT 15

Gives me the following:

threadid
1355
140
1321
1338
140
1334
1333
1311
1332
1319
1311
1284
1312
1311
1308

Those are my actual last 15 posts. Now if I add distinct, this is what I get:

SELECT distinct threadid FROM post WHERE userid = 1 ORDER BY postid DESC LIMIT 15

threadid
1355
1321
1338
1334
1333
1332
1319
1284
1312
1311
1310
1309
1308
1300
1288

Notice all the lower threadid's are not included.

Any ideas?
Reply With Quote
  #5  
Old 03-08-2007, 04:55 PM
TECK's Avatar
TECK TECK is offline
 
Join Date: Nov 2001
Location: Canada
Posts: 4,182
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

For sure is not showing the same threadid's, you removed all duplicates so it gives room for other id's. For example 140 (2times), 1311 (3times), etc.
Also, you forgot to set a dateline in your query.
You must set a dateline, orelse you endup scanning the hole table for your id's, then drop all of them and keep only 5-15, whatever you want there.
This is very unorthodox, from a coder point of view, not to say very bad.
Reply With Quote
  #6  
Old 03-08-2007, 05:13 PM
Paul M's Avatar
Paul M Paul M is offline
 
Join Date: Sep 2004
Location: Nottingham, UK
Posts: 23,748
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I suggest you output the postids so you can see better what is going on.
Reply With Quote
  #7  
Old 03-09-2007, 09:01 PM
Mistah Roth's Avatar
Mistah Roth Mistah Roth is offline
 
Join Date: Jul 2003
Location: Ottawa, Ontario
Posts: 70
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by TECK View Post
For sure is not showing the same threadid's, you removed all duplicates so it gives room for other id's. For example 140 (2times), 1311 (3times), etc.
It shows some doubles, 1311 is still in the second query, its just the lower values that get dropped, I don't know why?

Quote:
Also, you forgot to set a dateline in your query.
You must set a dateline, orelse you endup scanning the hole table for your id's, then drop all of them and keep only 5-15, whatever you want there.
This is very unorthodox, from a coder point of view, not to say very bad.
Good call, thanks for pointing that out haha
Reply With Quote
  #8  
Old 03-09-2007, 09:31 PM
TECK's Avatar
TECK TECK is offline
 
Join Date: Nov 2001
Location: Canada
Posts: 4,182
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

It shows some doubles, 1311 is still in the second query, its just the lower values that get dropped, I don't know why?
Show me the query and the results.
Use EMS MySQL Manager to see all about your query (and post screenshots), it's free.

EMS SQL Manager 2005 Lite for MySQL, Windows edition (full installation package)
http://sqlmanager.net/en/products/my...nager/download
Reply With Quote
  #9  
Old 03-12-2007, 01:20 AM
Mistah Roth's Avatar
Mistah Roth Mistah Roth is offline
 
Join Date: Jul 2003
Location: Ottawa, Ontario
Posts: 70
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

My 4th post in this thread has the queries and results.
Reply With Quote
  #10  
Old 03-12-2007, 01:58 AM
Cap'n Steve's Avatar
Cap'n Steve Cap'n Steve is offline
 
Join Date: Feb 2004
Location: Kalamazoo, MI, USA
Posts: 745
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't know anything about DISTINCT, but this might work:

[sql]SELECT threadid FROM post WHERE userid = 1 GROUP BY threadid ORDER BY postid DESC LIMIT 5[/sql]
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 12:51 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04011 seconds
  • Memory Usage 2,260KB
  • Queries Executed 11 (?)
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
  • (2)bbcode_code
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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_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
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete