Jump to content

Ordering Results From a Query


limitphp

Recommended Posts

I have a table called security_questions.  It has two columns, questionID and question.

 

Is there a way to select records from the security_questions table but order the results so that a certain record is first?

 

I want the first record to be the one where questionID = $securityAnswer

 

Is that possible?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/142824-ordering-results-from-a-query/
Share on other sites

basically, I want to return all records from a table, but I want a certain record to be first.

 

Can you do that with a query?

 

or would you need two queries......one where you select the record you want and display it...and then another right after to display all the other records?

you should modify posts instead of posting your own thread .. makes us think people are helping you already when they really haven't.

 

the easy way would be to do this in two queries, but you should be able to do it with and groupr by modifier of some type

 

http://dev.mysql.com/doc/refman/5.1/en/group-by-modifiers.html

 

 

or better yet if you are getting the same information for all the items in the query you can do a UNION

 

http://dev.mysql.com/doc/refman/5.1/en/union.html

 

 

 

 

you should modify posts instead of posting your own thread .. makes us think people are helping you already when they really haven't.

 

the easy way would be to do this in two queries, but you should be able to do it with and groupr by modifier of some type

 

http://dev.mysql.com/doc/refman/5.1/en/group-by-modifiers.html

 

 

or better yet if you are getting the same information for all the items in the query you can do a UNION

 

http://dev.mysql.com/doc/refman/5.1/en/union.html

 

 

 

 

 

Are you sure a group by can do it?

I don't want to group by a column.....

I want a certain record to be first, and then show all the other records after it.

 

Basically, I have about 7 entries in the security_questions table....

and on the myaccount page, if they select a different security question via the select input form, I want it to put that question on the top of the list....

 

this is what I'm doing now:

$query_new_Question = "SELECT questionID, question FROM security_questions WHERE questionID = '$new_question'";
$result_new_question = mysql_query($query_new_Question) or die (mysql_error());
$row_new_question = mysql_fetch_assoc($result_new_question);
echo "<OPTION VALUE='$row_new_question[questionID]'>$row_new_question[question]</OPTION>";

$queryQuestions = "SELECT * FROM security_questions";
$resultQuestions = mysql_query($queryQuestions) or die (mysql_error());
while ($dataQuestions = mysql_fetch_assoc($resultQuestions))
{									
if ($dataQuestions[questionID]!=$row_new_question[questionID])
{
echo "<OPTION VALUE='$dataQuestions[questionID]'>$dataQuestions[question]</OPTION>";
}
}

 

I'm just wondering if I could combine those two queries into one?

using a union would probably be the best way your query would look something like this.

 

$query = "SELECT questionID, question FROM security_questions WHERE questionID = '$new_question'
              UNION
              SELECT questionID, question FROM security_questions WHERE questionID != '$new_question'";

 

That should get you the first item followed by the rest.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.