LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2011, 05:35 AM   #1
Skyer
Member
 
Registered: Aug 2011
Posts: 113

Rep: Reputation: 6
MySQL make a row first in result of query


Hello,
I am writing simple CMS which integrates sorting menu based on current category. (So menu for currect category is always first).

It uses MySQL to get menu entries. Entries are placed in ul list, one li for each entry.

Now, I would like to get one entry, based on currect category, in the top of result.

I know there are more ways to approach this using php, but I'd like to keep this logical and systematic - just move the row to the top of query's result.

I'm not very friendly with SQL, so I'd appreciate if you give me some suggestion on how to do it?



Thanks,

Skyer
 
Old 08-12-2011, 05:57 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

You can do this with some extra "order by" clause. For example, you have a table with two columns, id and name. You want it sorted by name, but the row with id=2 should be first:

Code:
create table test (id int not null,name varchar(255));
insert into test (id,name) value (1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'a'),(6,'b');
select id,name from test order by (id=2) desc,name;
 
Old 08-12-2011, 06:31 AM   #3
Skyer
Member
 
Registered: Aug 2011
Posts: 113

Original Poster
Rep: Reputation: 6
Thanks,
that's exactly what I was looking for.


Skyer
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Change a mysql query result prior to display hattori.hanzo Programming 4 10-19-2010 05:46 AM
qpopper mysql "sql query fetch row failed ()" hunterpoint Linux - Server 0 09-14-2010 12:12 AM
Apache2, PHP5, or MYSQL issue: Unable to jump to row 0 on MySQL ..... MTAS Linux - Server 5 03-26-2010 09:13 AM
MySQL - Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 9 jasontn Linux - Software 0 02-09-2005 12:17 PM
mySQL redirecting query result to a .txt file! buttersoft Linux - Software 8 12-12-2003 03:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration