ORDER BY soundex with WHERE (MySql) - mysql

Query code:
SELECT *
FROM example
WHERE name LIKE '%test%'
OR SOUNDEX(name) LIKE 'T230%'
OR SOUNDEX(name) LIKE 'T23%'
I want to show first the results matched with WHERE name LIKE '%test%' and after SOUNDEX(name) LIKE 'T230%' and the lasts rows is the result of SOUNDEX(name) LIKE 'T23%'
Thank you for the attention.

You can use boolean expressions in order by. The "true" is treated as "1" and false as "0". So:
ORDER BY (name LIKE '%test%') DESC,
(SOUNDEX(name) LIKE 'T23%') DESC,
(SOUNDEX(name) LIKE 'T230%') DESC

Related

Custom Sort By "%%" in mysql

I have a table like this:
|city_name|
Shadyside
Chalkyitsik
Wyalusing
Quartzsite
Seaside
Shawnee Mission
Siberia
Sibley
Nicasio
Lacassine
Sicily Island
Andalusia
Sidell
Sidney
...
..
.
And selected with the following queries:
SELECT city_name FROM my_table WHERE city_name LIKE '%si%'
Instead of '%si%', anything can be put.
I want to sort by the words first started with 'si'.
And the output is like this:
|city_name|
Siberia
Sibley
Sicily Island
Sidell
Sidney
... And the rest of the words that are '%si%'
How should this sorting(ORDER BY) be done?
You can use multiple keys in the order by and expressions too:
SELECT city_name
FROM my_table
WHERE city_name LIKE '%si%'
ORDER BY (city_name LIKE 'si%') DESC, city_name;
MySQL treats boolean expressions as numbers in a numeric context, with "0" for false and "1" for true. The DESC puts the matches (1 = true) first.
SELECT city_name
FROM my_table
WHERE city_name LIKE '%si%'
ORDER BY (city_name LIKE 'si%') DESC;

MySQL "Order By" query

I have the select query with or logical operator like following:
SELECT * FROM grabli_new.product where article like '%AV2%' or article like '%AV22%';
I need to ordering result set by "length of like pattern"(rows that contains the longest pattern in my case '%AV22%' must be in the beginning of result set). The query must return all result that contains '%AV22%' pattern in the beginning and only then all result that contains '%AV2%' pattern. Is it possible?
You can try to use something like this:
SELECT *, case when article like '%AV22%'
then 1
when article like '%AV2%'
then 2 end orderIndex
FROM grabli_new.product
where article like '%AV2%' or article like '%AV22%'
order by orderIndex
if you want to count (multi-byte) characters, use the CHAR_LENGTH function instead:
ORDER BY CHAR_LENGTH( column )
SELECT *
FROM grabli_new.product
WHERE article LIKE '%AV2%' OR article LIKE '%AV22%'
ORDER BY char_length(article) DESC

MySQL SELECT "LIKE" parameter not working properly

I am building a search function in my "search.php" for a database in which I am storing information on ip-addresses and various units. Each unit has a "category" column in the database. My search choices are either searching in a text field, and/or selecting a category in a drop-down list. When I choose a category in my drop-down list and not typing anything in the text field, I get the correct results (returning all rows containing the selected category).
But when I type anything in the text field, I don't get any results.
Query when I ONLY choose category(in this case "IP-adress" is chosen as category):
SELECT dataid,
firstname,
lastname,
data.name dataname,
description,
ip,
TYPE,
TIMESTAMP
FROM DATA
INNER JOIN USER ON DATA.userid=USER.userid
WHERE TYPE='IP-adress'
AND DATA.name LIKE '%%'
AND description LIKE '%%'
AND ip LIKE '%%'
AND TYPE LIKE '%%'
AND firstname LIKE '%%'
AND lastname LIKE '%%'
ORDER BY TIMESTAMP DESC
This is returning all rows containing the selected category, as I want to.
Query when I type something in the textfield:
SELECT dataid,
firstname,
lastname,
data.name dataname,
description,
ip,
TYPE,
TIMESTAMP
FROM DATA
INNER JOIN USER ON DATA.userid=USER.userid
WHERE TYPE='IP-adress'
AND DATA.name LIKE '%test%'
AND description LIKE '%test%'
AND ip LIKE '%test%'
AND TYPE LIKE '%test%'
AND firstname LIKE '%test%'
AND lastname LIKE '%test%'
ORDER BY TIMESTAMP DESC
I have combinations where I have columns containing the word "test" just as my input, but I get no results.
I have been at it quite some time now and I am quite sure I had the opposite problem before, where I didn't get any results when I chose a category.
Anyway, I hope another set of eyes could help me out here. If you need more code, just ask for it!
This is because you have used AND for all the condition. It will search for test in all fiends and if one field does not have test then condition fails.
Try this way
SELECT dataid,
firstname,
lastname,
data.name dataname,
description,
ip,
TYPE,
TIMESTAMP
FROM DATA
INNER JOIN USER ON DATA.userid=USER.userid
WHERE TYPE='IP-adress'
AND (DATA.name LIKE '%test%'
OR description LIKE '%test%'
OR ip LIKE '%test%'
OR TYPE LIKE '%test%'
OR firstname LIKE '%test%'
OR lastname LIKE '%test%')
ORDER BY TIMESTAMP DESC
If I'm looking at the second query:
WHERE type='IP-adress'
and later on:
AND type LIKE '%test%'
That wouldn't give any result back as 'IP-adress' doesn't contain 'test' in it...
Also, the second query means actually that ALL the fields which are given in the where should contain at least the text 'test'. Are you sure that is correct?

Ordering SQL results by the field where they are found

We have a search query that allows the users to enter a string and then searches 3 separate fields via LIKE '%string%'. The fields are:
Name
Description
Type
When then return the results and right now just order them arbitrarily. We would prefer the results be ordered first by results found in the Name field, second by items found in the Description field, and then lastly in the Type field. Because we have paging and sorting also associated with this, we really want it returned in one record set. Is this even possible?
Thanks in advance.
In MySQL, the following should work:
SELECT *
FROM atable
WHERE Name LIKE '%string%'
OR Description LIKE '%string%'
OR Type LIKE '%string%'
ORDER BY
CASE
WHEN Name LIKE '%string%' THEN 1
WHEN Description LIKE '%string%' THEN 2
WHEN Type LIKE '%string%' THEN 3
END
;
Please try this :
Select *
from Table
where Name like '%string%'
and description like '%string%'
and type like '%string%'
order by name,description,type
You can try this:
Select *
from Tablename
where Name like '%string%'
and description like '%string%'
and type like '%string%'
order by name,description,type

mysql alphabetical order

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.