Say I have a table similar to:
ID Name
1 Test
2 Contest
3 Fittest
4 Testament
Is there a MySQL query I could use with ordering to allow it to display a specific word first?
For example, users are searching for the word "Test". I have a statement similar to "SELECT * FROM table WHERE NAME LIKE '%Test%'". Could I display results to show things that START with Test begin first followed by everything else, while everything is still in alphabetical order.
So output would be:
Test
Testament
Contested
Fittest
Thanks.
This will put your words that begin with Test at the top, and sort those words plus the remainder of the list in alphabetical order..
SELECT * FROM mytable
ORDER BY CASE WHEN name LIKE 'test%' THEN 0 ELSE 1 END ASC, name ASC
SELECT * FROM table
WHERE NAME LIKE '%Test%'
order by case when name like 'test%' then 0 else 1 end
Related
I want SQL to show / order the results for the column name first then show results for the description column last.
Current SQL query:
SELECT * FROM products WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
I tried adding order by name, description [ASC|DESC] on the end but that didn't work.
It's for optimizing the search results. If a certain word is found in description it should go last if a certain word is also found in the name column.
You can use a CASE statement in an ORDER BY to prioritize name. In the example below all results where name is matched will come first because the CASE statement will evaluate to 1 whereas all other results will evaluate to 2.
I'm not sure by your problem description what exactly you want the behavior to be, but you can certainly use this technique to create more refined cases to prioritize your results.
SELECT *
FROM products
WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
ORDER BY CASE WHEN name LIKE '%$search_query%' THEN 1 ELSE 2 END
If you want the names first, the simplest order by is:
order by (name like '%$search_query%') desc
MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.
While this is undocumented, when results sets combined by a UNION ALL and not sorted afterwards, they stay in the order returned, as UNION ALL just adds new results to the bottom of the result set. This should work for you:
SELECT * FROM products
WHERE name LIKE '%$search_query%'
UNION ALL
SELECT * FROM products
WHERE (description LIKE '%$search_query%' AND name NOT LIKE '%$search_query%')
I have a mysql query like :
SELECT name FROM table_name WHERE name LIKE '%custom%' limit 10
It retruns me 2 rows from my custom table.
I want to get records which contains either of any word from the text c cu cus cust usto stom tom om m also.
I tried below query :
SELECT name FROM table_name WHERE name like '%custom%' OR name REGEXP 'c|cu|cus|cust|usto|stom|tom|om|m' limit 10
Above query returning me 7 records but these 7 records does not have such 2 records which 1st query result have.
How to get that? Or any other way to get these result in mysql?
EDIT : Here I also want to order by maximum substrings matches in second query.
Try this:
SELECT name FROM table_name WHERE name REGEXP 'custom' limit 10;
There is no need of LIKE with REGEXP, but REGEXP are slower then LIKE. So if your table have so many records then REGEXP quesries are slower.
Try this:
SELECT name FROM table_name WHERE name REGEXP 'custom|c|cu|cus|cust|usto|stom|tom|om|m' limit 10
What we did above is that we combined custom with the rest of the patterns, and we made them all use REGEXP.
You need to add word boundaries, which in MySQL are [[:<:]] for start of word and [[:>:]] for end of word:
SELECT name
FROM table_name
WHERE name REGEXP '[[:<:]](c|cu|cus|cust|usto|stom|tom|om|m)[[:>:]]'
limit 10
See live demo.
Note the brackets around the alternation.
I have an ajax-search on a mysql-db. Example: Search for "man" which I query with:
SELECT id FROM table WHERE name LIKE '%man%;
I now want to sort the result to have all results starting with the search in alphabetical order:
man
mankind
after that I want to have all results width the search INSIDE in alphabetical order, like:
iron man
woman
How can I do that?
You can order by the position of your search term in the string:
SELECT id
FROM table
WHERE name LIKE '%man%'
ORDER BY INSTR(name, 'man'), name
See also: INSTR(), LOCATE()
You could also change the expression to only distinguish between start of the string or anywhere else:
ORDER BY IF(INSTR(name, 'man'), 1, 0)
You can construct your ORDER BY using a CASE statement to verify the substrings. Note: I am using UPPER() here to convert both the search value and the column value to uppercase, for a case-insensitive match. If that is not your need, remove the UPPER().
ORDER BY
CASE
/* Matches the start of the string */
WHEN UPPER(LEFT(name, 3)) = 'MAN' THEN 1
/* Doesn't match the end or the start (in the middle) */
WHEN UPPER(RIGHT(name, 3)) <> 'MAN' THEN 2
/* Matches the end of the string */
WHEN UPPER(RIGHT(name, 3)) = 'MAN' THEN 3
ELSE 4
END,
/* Then order by the name column */
name
This method should be fairly portable, but I like the INSTR() answer below better.
Try this
SELECT id FROM table WHERE name LIKE 'man%';
UNION
SELECT id FROM table WHERE name LIKE '%man%';
Often, sorting is done with symbols sorted to the top, like 0 or * or &. This is the default way that mysql sorts; numbers and symbols and then A-Z. However, that makes the often ugliest or most badly formatted results float to the top (e.g. a result of ##$#3423 or 8 inch or &).
So I'd like to do a modified form of that, letters first A-Z, and then special characters last.
How would I go about creating that type of sort? Something in the ORDER BY clause?
Based on a google-cached link to this page:
http://www.google.com/url?sa=t&source=web&cd=3&ved=0CCUQFjAC&url=http%3A%2F%2Fblog.feedmarker.com%2F2006%2F02%2F01%2Fhow-to-do-natural-alpha-numeric-sort-in-mysql%2F&ei=Zg2_TZyKDaffiALjjqwo&usg=AFQjCNGS-rX7AmfrumXK8J7bVSj96bSSmQ
EDIT: Original link is dead.
Here is another link which actually explains what is happening better than the first link did:
http://matthewturland.com/2008/11/05/natural-ordering-in-mysql/
You might try this
SELECT names FROM your_table ORDER BY names + 0 ASC
Select ...
From ...
Order By Case When Col Like '[0-9]%' Then 1 Else 0 End Asc
, Col
Another solution that would account for special characters:
Select ...
From ...
Order By Case When Col Like '[A-Z]%' Then 0 Else 1 End Asc
, Col
This works for me:
SELECT * FROM `yourTable` ORDER BY `yourDatabase`.`yourColumn` ASC
i am trying to sort mysql data with alphabeticaly like
A | B | C | D
when i click on B this query runs
select name from user order by 'b'
but result showing all records starting with a or c or d i want to show records only starting with b
thanks for help
i want to show records only starting with b
select name from user where name LIKE 'b%';
i am trying to sort MySQL data alphabeticaly
select name from user ORDER BY name;
i am trying to sort MySQL data in reverse alphabetic order
select name from user ORDER BY name desc;
but result showing all records
starting with a or c or d i want to
show records only starting with b
You should use WHERE in that case:
select name from user where name = 'b' order by name
If you want to allow regex, you can use the LIKE operator there too if you want. Example:
select name from user where name like 'b%' order by name
That will select records starting with b. Following query on the other hand will select all rows where b is found anywhere in the column:
select name from user where name like '%b%' order by name
You can use:
SELECT name FROM user WHERE name like 'b%' ORDER BY name
If you want to restrict the rows that are returned by a query, you need to use a WHERE clause, rather than an ORDER BY clause. Try
select name from user where name like 'b%'
You do not need to user where clause while ordering the data alphabetically.
here is my code
SELECT * FROM tbl_name ORDER BY field_name
that's it.
It return the data in alphabetical order ie; From A to Z.
:)
I had the same challenge, but after little research I came up with this and it gave me what I wanted, and I was able to overcome that path.
SELECT * from TABLE ORDER BY name
Wildcard Characters are used with like clause to sort records.
If we want to search a string which is starts with B then code is like the following:
select * from tablename where colname like 'B%' order by columnname ;
If we want to search a string which is ends with B then code is like the following:
select * from tablename where colname like '%B' order by columnname ;
If we want to search a string which is contains B then code is like the following:
select * from tablename where colname like '%B%' order by columnname ;
If we want to search a string in which second character is B then code is like the following:
select * from tablename where colname like '_B%' order by columnname ;
If we want to search a string in which third character is B then code is like the following:
select * from tablename where colname like '__B%' order by columnname ;
Note : one underscore for one character.
I try to sort data with query it working fine for me please try this:
select name from user order by name asc
Also try below query for search record by alphabetically
SELECT name FROM `user` WHERE `name` LIKE 'b%'
MySQL solution:
select Name from Employee order by Name ;
Order by will order the names from a to z.