Jump to content

ORDER BY FIELD() not working


Go to solution Solved by BuildMyWeb,

Recommended Posts

mysql v5.5.34-cll

 

this query works:

SELECT t_u.*, t_a.access_lvl, t_a.biz_name from uploads_rf AS t_u, user_accts AS t_a WHERE t_u.project_id = '8' AND t_u.pipeline_item = 'rf' AND t_a.email = t_u.author ORDER BY status

there are three status values.  'open', 'closed', 'rejected'.

 

when i try sorting by the status column according to value as in such, i get 0 results. 

query_docs=SELECT t_u.*, t_a.access_lvl, t_a.biz_name from uploads_rf AS t_u, user_accts AS t_a WHERE t_u.project_id = '8' AND t_u.pipeline_item = 'rf' AND t_a.email = t_u.author ORDER BY FIELD(status, 'open', 'rejected', 'closed')

is my mysql written correctly?  any thoughts?

Link to comment
https://forums.phpfreaks.com/topic/285346-order-by-field-not-working/
Share on other sites

Since I don't know the values of status field, I'm going to use the default 1,2,3 in a CASE.

 

Try,

SELECT t_u.*, t_a.access_lvl, t_a.biz_name,
CASE status 
     WHEN 'open' THEN 1
     WHEN 'rejected' THEN 2 
     WHEN 'closed' THEN 3
  END AS s_type
FROM uploads_rf AS t_u, user_accts AS t_a 
WHERE t_u.project_id = '8' AND t_u.pipeline_item = 'rf' AND t_a.email = t_u.author 
ORDER BY FIELD(status, 'open', 'rejected', 'closed')

Edited by jazzman1

works for me

mysql> SELECT * FROM sort_test;
+-------------+----------+
| idsort_test | status   |
+-------------+----------+
|           1 | open     |
|           2 | rejected |
|           3 | closed   |
|           4 | open     |
|           5 | rejected |
|           6 | closed   |
|           7 | open     |
|           8 | rejected |
|           9 | closed   |
+-------------+----------+

mysql> SELECT * FROM monty.sort_test ORDER BY FIELD(status,'open','rejected','closed');
+-------------+----------+
| idsort_test | status   |
+-------------+----------+
|           1 | open     |
|           4 | open     |
|           7 | open     |
|           2 | rejected |
|           5 | rejected |
|           8 | rejected |
|           3 | closed   |
|           6 | closed   |
|           9 | closed   |
+-------------+----------+

It shouldn't affect the sort but your query has no join condition on those tables

thanks for the replies guys. 

 

jazzman: havent used case statements in mysql before but ill read up on them.  its an option but not required for the FIELD() function to work properly, correct?

 

barand, thx for the alert on join condition.  sounds like youre saying my statement should be working though.  i cant figure out why im getting 0 results with it.  if i use the same exact query but switch from ORDER BY FIELD() to ORDER BY, it works great.  i dont know where to go from here.

 

there shouldnt be a compatibility issue with the FIELD() function and my version of mysql, should there?

The FIELD() function in the manuals is unchanged from v3.23 through to v5.7.

 

There is a similar function FIND_IN_SET(). Any luck with that one? eg

mysql> SELECT * FROM sort_test
    -> ORDER BY FIND_IN_SET(status,'open,rejected,closed');
+-------------+----------+
| idsort_test | status   |
+-------------+----------+
|           1 | open     |
|           4 | open     |
|           7 | open     |
|           2 | rejected |
|           5 | rejected |
|           8 | rejected |
|           3 | closed   |
|           6 | closed   |
|           9 | closed   |
+-------------+----------+

How is "status" defined in your table?

Name Type Collation   Null Default

status char( 8 ) utf8_bin   No open

$num_rows = $result_docs->num_rows;

echoing $num_rows returns a NULL, not zero results. 

 

FIND_IN_SET() had same result

Edited by BuildMyWeb
echoing $num_rows returns a NULL, not zero results.

 

 

your query is failing due to an error (or you are using the wrong variable, overwriting the variable...) do you have any error checking logic in your code so that you would know when and why your query is failing?

  • Solution

a friend got the answer real quick.  was simple, now that i see it.  wasnt identifying the table for 'status'.  the following works:

SELECT t_u.*, t_a.access_lvl, t_a.biz_name from uploads_rf AS t_u, user_accts AS t_a WHERE t_u.project_id = '8' AND t_u.pipeline_item = 'rf' AND t_a.email = t_u.author ORDER BY FIELD(t_u.status, 'open', 'rejected', 'closed')

note: ORDER BY FIELD(t_u.status, ...

 

thanks for the help everyone

Edited by BuildMyWeb
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.