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;
Related
SELECT * FROM Customers
ORDER BY Country>1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
I want to know when that compares the value of Country and 1000...,
1:what is the result after comparison?
2:what is the value of Country in this comparasion(askicode or....)?
(Country is string like usa uk and some thing like that)
Do this select:
SELECT *, Country>1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 as Comparision
FROM Customers
ORDER BY Country>1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
This will add a new column, with the name (=alias) Comparison. This will show the value of that expression.
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
I have this MYSQL table named people with the columns: id|firstname|lastname|birthdate|phone.
I am quite new to MYSQL and I'm trying to UNION several SELECTs so that the result will look in the following way:
only the first 20 results must be shown
the first SELECT criteria is by the combination firstname+lastname+birthdate: WHERE (birthdate="1980-01-01") AND ((firstname LIKE "%john%") AND (lastname LIKE "%smith%"))
the second SELECT criteria is by the combination firstname+lastname: WHERE (firstname LIKE "%john%") AND (lastname LIKE "%smith%")
the third SELECT criteria is by phone: WHERE phone="0123456"
the output result must in fact have 3 columns: order|id|type; where "order" and "type" are alias columns
the exported ids must be unique: if the same id results from all the 3 SELECTs (or from more than one SELECT), then it must appear only once in the output table
the column "order" must have the value 1 for the results of the first SELECT, 2 for the 2nd SELECT and 3 for the last SELECT
if the same id value results from more than one SELECT, then its row must have the highest order value; where the highest order posible is 1, from the first SELECT
the alias column "type" must work like this: if an id results from the 1st SELECT, it's type value is "~firstname+lastname+birthdate~"; if an id results from the 2nd SELECT, it's type value is "~firstname+lastname~"; and finally if an id results from the 3rd SELECT, it's type value is "~phone~"
if the same id value results from more than one SELECT, the value on the "type" alias column must be a concatention between the SELECTs where that id was found (for example, if the same id resulted in all 3 SELECT queries then the value on the "type" column would be "~firstname+lastname+birthdate~~firstname+lastname~~phone~")
Is it possible to achieve such an output?
Here's something using CASE statements. I think you'll get into a mess with union statements, because of your order type statement. Hopefully I've understood what you're after - it's much easier if you post sample data! Anyway, even if this doesn't do exactly what you want, you get the idea....
[EDIT] I don't think you need the distinct, but I don't think it hurts, either...
SELECT DISTINCT
CASE WHEN birthdate='1980-01-01'
AND firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN 1
WHEN firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN 2
WHEN phone='0123456'
THEN 3
END AS outputorder, -- avoid confusion of using an SQL keyword as a column name,
id,
CONCAT(
CASE
WHEN birthdate='1980-01-01'
AND firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN CONCAT('~',firstname,'+',lastname,'+','~')
END ,
CASE WHEN firstname LIKE '%john%' AND lastname LIKE '%smith%'
THEN CONCAT('~',firstname,'+',lastname,'~')
END ,
CASE WHEN phone='0123456'
THEN CONCAT('~',phone,'~')
END
) -- end the concat
AS outputtype
FROM
mytable
WHERE
( birthdate='1980-01-01'
AND firstname LIKE '%john%' AND lastname LIKE '%smith%')
OR
(firstname LIKE '%john%' AND lastname LIKE '%smith%')
OR
phone='0123456'
ORDER by 1,2 LIMIT 20
In the end I did something like this and it worked just fine:
SELECT MIN(`ord`) AS `order` , `id` , GROUP_CONCAT(`spec`) as `type` FROM (
SELECT "1" AS `ord` , `id` , "~firstname+lastname+birthdate~" AS `spec` FROM `people` WHERE (`birthdate` = "1986-04-02") AND (`lastname` LIKE "%smith%") AND (`firstname` LIKE "%john%")
UNION
SELECT "2" AS `ord` , `id` , "~firstname+lastname~" AS `spec` FROM `people` WHERE (`lastname` LIKE "%smith%") AND (`firstname` LIKE "%john%")
UNION
SELECT "3" AS `ord` , `id` , "~phone~" AS `spec` FROM `people` WHERE (`phone`="0123456")
) final
GROUP BY final.`id`
ORDER BY `order` ASC
LIMIT 20
Thanks to mlinth for the alternative though...
I wonder if its possible to sort a result so that if a column contains a spesific word ('misc'), it will come out last?
current query:
select * from table order by name asc
current result:
banjo
guitar
miscproduct1
miscproduct2
piano
pseudocode:
select * from table order by name asc,
except if name like '%misc%' then sort it last
pseudo result:
banjo
guitar
piano
miscproduct1
miscproduct2
Is this possible?
You could use LOCATE() as the first ordering argument:
ORDER BY LOCATE('misc', name), name;
The LOCATE() function returns 0 if the search string can't be found in the subject, and yields a positive integer if it is found.
To normalize the sorting for subjects that contain the search you can combine with LEAST():
ORDER BY LEAST(LOCATE('misc', name), 1), name;
You can use a CASE in the ORDER BY clause.
SELECT colName
FROM table
ORDER BY
CASE WHEN colName LIKE 'misc%'
THEN concat('zzz',colName)
ELSE colName
END
Demo: http://sqlfiddle.com/#!2/6e08c/7
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.