Helo,
I have a question regarding how to sort out SQL search result.
FYI I'm using MySQL 4.1
Let's assume I have the following table 'blah':
Now I want to look for all the record that contains the word Joe.
Should be easy with the following:
SELECT * FROM blah WHERE name LIKE '%JOE%'
With the following result:
2 Eat at Joe's
3 Don't eat at Joe's
5 Joe blow
6 Joe let go
Now, if I want to sort it such that records that begin with 'Joe' proceeds other records, how would I go about it? Basically, I want the output to look like this:
5 Joe blow
6 Joe let go
3 Don't eat at Joe's
2 Eat at Joe's
(The order of 3 and 2 doesn't matter, but it's great if I can start with D before E)
I tried SELECT * FROM blah WHERE name LIKE 'JOE%' OR name LIKE '%JOE%' ORDER BY name but it gave me the records 3, 2, 5, 6.
Well, this could be done with two queries:
SELECT * FROM blah WHERE name LIKE 'JOE%' ORDER BY name
SELECT * FROM blah WHERE name LIKE '_%JOE%' ORDER BY name
But I would like to avoid that if possible (ie: Do the above in one command)
Any hints or ideas?
Thanks.
-stndn.
I have a question regarding how to sort out SQL search result.
FYI I'm using MySQL 4.1
Let's assume I have the following table 'blah':
id | name
---+------
1 | Don't eat at Mike's
2 | Eat at Joe's
3 | Don't eat at Joe's
4 | Mike eat at Mike's
5 | Joe is not Mike
6 | Joe let go of Mike
Now I want to look for all the record that contains the word Joe.
Should be easy with the following:
SELECT * FROM blah WHERE name LIKE '%JOE%'
With the following result:
2 Eat at Joe's
3 Don't eat at Joe's
5 Joe blow
6 Joe let go
Now, if I want to sort it such that records that begin with 'Joe' proceeds other records, how would I go about it? Basically, I want the output to look like this:
5 Joe blow
6 Joe let go
3 Don't eat at Joe's
2 Eat at Joe's
(The order of 3 and 2 doesn't matter, but it's great if I can start with D before E)
I tried SELECT * FROM blah WHERE name LIKE 'JOE%' OR name LIKE '%JOE%' ORDER BY name but it gave me the records 3, 2, 5, 6.
Well, this could be done with two queries:
SELECT * FROM blah WHERE name LIKE 'JOE%' ORDER BY name
SELECT * FROM blah WHERE name LIKE '_%JOE%' ORDER BY name
But I would like to avoid that if possible (ie: Do the above in one command)
Any hints or ideas?
Thanks.
-stndn.