SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 expert on "order by"
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

leracj
Starting Member

Philippines
13 Posts

Posted - 06/29/2013 :  13:26:38  Show Profile  Reply with Quote
i have a table here:


i need to "order it by first_name"
so that when i run a query like

SELECT meta_value FROM wp_usermeta

where the top of the result will be all names in ORDER BY ASC



please help

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/29/2013 :  14:01:44  Show Profile  Reply with Quote


SELECT meta_value FROM wp_usermeta 
     ORDER BY (CASE WHEN meta_Key = 'first_name' THEN 1 ELSE 2 END),
     meta_value;


Go to Top of Page

leracj
Starting Member

Philippines
13 Posts

Posted - 06/29/2013 :  14:28:11  Show Profile  Reply with Quote
what does THEN 1 ELSE 2 mean?

quote:
Originally posted by MuMu88



SELECT meta_value FROM wp_usermeta 
     ORDER BY (CASE WHEN meta_Key = 'first_name' THEN 1 ELSE 2 END),
     meta_value;






please help
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/29/2013 :  14:54:16  Show Profile  Reply with Quote
The case statement returns 1 if the meta_key is 'first_name' else it returns 2.
When you pass these values to your order by clause it sorts the data with 'first_name' followed by all other data.
Go to Top of Page

leracj
Starting Member

Philippines
13 Posts

Posted - 06/29/2013 :  15:37:54  Show Profile  Reply with Quote

how about if i want to insert another row

basically i want to have a row for first name and another row for address
how do i do that?

my code:
SELECT DISTINCT ID, user_login, user_email, meta_value, COUNT( * ) , meta_key
FROM wp_users um, wp_usermeta u
WHERE (
user_login LIKE '%tho%'
OR meta_value LIKE '%tho%'
OR user_email LIKE '%tho%'
)
AND (
user_id = ID
AND (
meta_key = 'first_name'
OR meta_key = 'last_name'
OR meta_key = 'last_name'
OR meta_key = 'city'
OR meta_key = 'address'
OR meta_key = 'programs'
)
)
GROUP BY ID, user_login, user_email
HAVING COUNT( * ) >0
ORDER BY (

CASE meta_key
WHEN 'first_name'
THEN 1
ELSE 100
END
) ASC , meta_key ASC , meta_value
LIMIT 0 , 30

please help

Edited by - leracj on 06/29/2013 15:41:40
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17587 Posts

Posted - 06/30/2013 :  23:37:09  Show Profile  Reply with Quote
You are using MySQL ? for MySQL question, please post at dbforums.com

SQLTeam.com is on Microsoft SQL Server.


KH
Time is always against us

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2202 Posts

Posted - 07/01/2013 :  00:29:57  Show Profile  Reply with Quote
quote:
Originally posted by leracj


how about if i want to insert another row

basically i want to have a row for first name and another row for address
how do i do that?

my code:
SELECT DISTINCT ID, user_login, user_email, meta_value, COUNT( * ) , meta_key
FROM wp_users um, wp_usermeta u
WHERE (
user_login LIKE '%tho%'
OR meta_value LIKE '%tho%'
OR user_email LIKE '%tho%'
)
AND (
user_id = ID
AND (
meta_key IN ( 'first_name', 'last_name', 'city', 'address', 'programs'))
)
GROUP BY ID, user_login, user_email -- include these columns(meta_value, meta_key) in GROUP BY cluase to work out the query
HAVING COUNT( * ) >0
ORDER BY user_id, (
CASE meta_key
WHEN 'first_name'
THEN 1
ELSE 100
END
) ASC , meta_key ASC
LIMIT 0 , 30

please help


try out the above modified query...
>>how about if i want to insert another row?
Can you provide expected output?


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000