vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Sort by Field (https://vborg.vbsupport.ru/showthread.php?t=317736)

KGodel 03-11-2015 03:14 PM

Sort by Field
 
Hello friends.

I am trying to sort the results of a MySQL query I am processing. Here is some background.

I have a list of possible values for a column. This list has 3 values that are always possible, the rest of the values can changed. I want to sort by this column, but in a specific way.

I know I can do
Code:

ORDER BY FIELD(bla, 'val1', 'val2'), bla ASC
My question is this. Of the three values that are always variable, I want two at the top of the sort order and one at the bottom. Is this possible? Basically, sort should look like this:

ForeverValue1
ForeverValue2
Variables ASC
..
..
ForeverValue3

(If you want to know why I want this its because I am generating a dynamic page but I want to use as few loops as possible. I could easily loop through these fields how I wanted in php but if I can sort them beforehand it will save me on code and pageload time.)

kh99 03-11-2015 05:59 PM

I didn't know about FIELD, that's pretty cool. Anyway, maybe this:
Code:

ORDER BY FIELD( bla,  'val2', 'val1' ) DESC , FIELD( bla,  'val3'), bla

KGodel 03-11-2015 06:37 PM

You cannot use ASC or DESC after the FIELD sort, you have to use the field again and then use the ASC or DESC, but that will catch the value I want excluded and saved for last. :/ If I add another FIELD sort it will have already been sorted right?

kh99 03-11-2015 06:40 PM

Quote:

Originally Posted by KGodel (Post 2540156)
You cannot use ASC or DESC after the FIELD sort, you have to use the field again and then use the ASC or DESC, but that will catch the value I want excluded and saved for last. :/ If I add another FIELD sort it will have already been sorted right?

Well, I tested what I posted. But are you saying you can't because it doesn't do what you want? Then I guess maybe I didn't understand what you wanted.

KGodel 03-11-2015 06:41 PM

I basically have three values I want excluded from the typical alphabetical sorting of ORDER BY. I want 2 values to be sorted first and one value to be sorted last. The others change so I just want them to have a normal sort between the 2 first values and the last value. Instead of:

A
B
C
...
Z

Where val1, val2, and val3 are included, I want:

val1
val2
A
B
C
...
Z
val3

kh99 03-11-2015 06:56 PM

I thought that's what I did. You say above that you can't have ASC or DESC after FIELD, but I think you can. In fact if you don't it just defaults to ASC anyway.

KGodel 03-11-2015 06:59 PM

I tried what you wrote and it ended up shoving Val3 to the top no matter what. If I don't add DESC to the val1 and val2 then they don't show up at the top either, so I basically need a DESC, ASC, then DESC (to be sure val3 shows at the bottom) but it seems that if the middle is ASC then val3 gets caught.

But I though of an alternate way. I could simply create a string in the order I want and JUST do a field thing instead of handling it in the MySQL

kh99 03-11-2015 07:13 PM

Hmm...strange. I made a test table with color names and whatever I put as 'val1' and 'val2' end up at the top, and 'val3' ends up at the bottom, and the rest end up between, sorted alphabetically. But I guess there must be something different about your data or something.

Anyway, at least you figured out a way.


All times are GMT. The time now is 03: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.01058 seconds
  • Memory Usage 1,726KB
  • 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
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)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