Page 1 of 1
Query question
Posted: Wed Aug 16, 2006 8:45 pm
by Uranium-235
I have a very big table, one of the fields, can be one of 3 things (basically, this table is a job listing, and this perticular field is the employment agency office).
Is there any way, to in one query select *, of a perticular office first, then display every other office after that, alphabetically? It's hard to explain
Like I want Office 2's jobs displayed first, then I would want Office 1, then 3
or 3, then 1 and 2
do you get the idea? I know a way of doing this with multiple queries, but with how things are outputted in the script, that would be a serious pain. Is there any way to achieve this with one query?
thank you
Posted: Wed Aug 16, 2006 8:50 pm
by feyd
ORDER BY clause.
Posted: Wed Aug 16, 2006 8:59 pm
by Uranium-235
I thought you could only order by fields
how can I order by, if a field is a certain value?
Posted: Wed Aug 16, 2006 9:08 pm
by Christopher
$sql = "SELECT id,office_name FROM jobs WHERE officeID='$officeID' ORDER BY office_name";
Posted: Wed Aug 16, 2006 9:26 pm
by feyd
You can order by more than just fields. Provided the database can understand the expression it will try to honor it. Whether it affects the results or not, that's a different story.

Posted: Wed Aug 16, 2006 9:40 pm
by Uranium-235
arborint wrote:$sql = "SELECT id,office_name FROM jobs WHERE officeID='$officeID' ORDER BY office_name";
woulden't that just select the first office? what about all the other jobs with a different office then 'officeID' (actually this is just by name, but still)
Posted: Wed Aug 16, 2006 9:45 pm
by feyd
Here's a big ..push
Code: Select all
ORDER BY `officeID` = '2' DESC, `office_name`
Posted: Wed Aug 16, 2006 9:45 pm
by RobertGonzalez
Uranium-235 wrote:arborint wrote:$sql = "SELECT id,office_name FROM jobs WHERE officeID='$officeID' ORDER BY office_name";
woulden't that just select the first office? what about all the other jobs with a different office then 'officeID' (actually this is just by name, but still)
Yes this will only grab one office. You may need to look at a union query or a subselect query, or some other regex type query where you pull the office you want first, order it by whatever, then pull all the others that are not the first office and order that by whatever. Sounds uniony to me.
Posted: Wed Aug 16, 2006 9:54 pm
by nickvd
I'm not an sql mastah by any stretch, as a result, i would probably just use two queries, the first to obtain the office in question, and the second to get the rest...