vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Egad! How does one fetch ordered results from this query? (https://vborg.vbsupport.ru/showthread.php?t=55408)

Velocd 07-19-2003 02:21 AM

Egad! How does one fetch ordered results from this query?
 
I have what at first seemed to be a simple task, but now after countless attempts .. I can't figure it out. :speechless:

PHP Code:

$sql 'SELECT entryid, avgrating, num_ratings FROM entries'

Let's say I have these 3 entries in the table `entries` :

Code:

entryid    |    avgrating    |    num_ratings
-------------------------------------------
1                  1                3000
2                  9                500
3                  9                1

I would like to somehow by modifying the query above, to return the results like this:

Code:

entryid    |    avgrating    |    num_ratings
-------------------------------------------
2                  9                500
1                  1                3000
3                  9                1

Basically this is ordering by the maximum avgrating and the maximum num_ratings , with some comparison in between though. For example, entryid 3 and its avgrating of 9 is larger than avgrating 1, but technically since entryid 1 has 3,000 ratings to it, it could have had 60 votes that were all 9's.

Hopefully this is making some logic. I can't just do a simple query and order it by avgrating , because an entry with an avgrating of 10 and one vote would display above an entry with an avgrating of 9 and 500 votes.

I can't order by num_ratings , because an entry, like entryid 1 in the example, would appear above an entry like entryid 2.

The solution is probably something simple I've just overlooked, as usual.. but I need to get it done in one query (if possible), and not have to make 2 queries with extra PHP code in between. Help would be much appreciated. ;)

Xenon 07-19-2003 09:08 AM

you can try this method:

PHP Code:

$sql 'SELECT entryid, avgrating, num_ratings, avgrating*num_ratings AS ordervalue FROM entries ORDER BY ordervalue'


Velocd 07-19-2003 04:04 PM

Thanks xenon, the method in theory is correct.. don't know why it didn't occur to me before, but a more complex algorithm is still needed... for if I had:

1 Avgrating - 4800 Results (product of 4800)
9 Avgrating - 500 Results (product of 4500)

:dead:

Velocd 07-19-2003 04:35 PM

I modified the query to look like:
PHP Code:

SELECT *,((avgrating*num_ratings)*(avgrating/2)) AS score FROM entries ORDER BY score 

And this seems to fix the problem I posted above... not sure if it is the best solution (or if the math is even correct), but.. for now it'll do.

Xenon 07-20-2003 11:02 AM

you're welcome :)

i just wanted to give the idea, the algorithm depends on how strong which value should be, so i couldn't help further :)


All times are GMT. The time now is 04:25 PM.

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.01677 seconds
  • Memory Usage 1,725KB
  • 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_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)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
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete