vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   SELECT DISTINCT problems (https://vborg.vbsupport.ru/showthread.php?t=141348)

Mistah Roth 03-06-2007 10:02 PM

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.

Zachariah 03-06-2007 11:40 PM

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'] . "

Marco van Herwaarden 03-07-2007 06:40 AM

Quote:

Originally Posted by Zachariah (Post 1197590)
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?

Mistah Roth 03-08-2007 04:26 AM

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?

TECK 03-08-2007 04:55 PM

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. :)

Paul M 03-08-2007 05:13 PM

I suggest you output the postids so you can see better what is going on.

Mistah Roth 03-09-2007 09:01 PM

Quote:

Originally Posted by TECK (Post 1198921)
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

TECK 03-09-2007 09:31 PM

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

Mistah Roth 03-12-2007 01:20 AM

My 4th post in this thread has the queries and results.

Cap'n Steve 03-12-2007 01:58 AM

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]


All times are GMT. The time now is 05:26 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.01863 seconds
  • Memory Usage 1,738KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_code_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete