I have a table with id, surname etc., when I query order by surname I obtain something like this
A, B, C,... V, Z, Ć, Č, Č, Đ ...
where slavian char are ordered after finished western european alphabet
Mysql connection is set in utf-8. query is
SELECT * FROM table ORDER BY SURNAME ASC
how can i obtain an unique order like
A, B, C, Ć, Č, Č, Đ... V, Z
Thanks
in this case you need to define your own Alphabet (or Order). To do so you can follow the answer to this question.
Hopefully it works with non-ascii signs.
What you need is to use proper collation, which provides necessary info about ordering strings. Here's how to use collate keyword:
SELECT * FROM table ORDER BY SURNAME COLLATE latin1_german2_ci
Here I used latin1_german2_ci as an example, find collation that matches your requirements.
Supported Character Sets and Collations
Related
I want to perform case-insensitive ORDER BY in MySQL.
I have the data in my database like
A, C, b, e, D etc
I'm getting the result as
A, C, D, b, e
But, I want the result as
A, b, C, D, e
How can I get that?
Choose a case-insensitive collation
select * from your_table
order by your_column COLLATE utf8_general_ci
That way indexes still work and the query is fast.
You can use
Select col
from myTable
order by lower(col)
That way it will compare all by lower values.
as #juergen d commented this will void indexes and therefor perfom slowly
There (at least) 3 solutions. Two (LOWER() and ORDER BY .. COLLATE ..) have already been given. Here is a third.
If the COLLATION of the column in question is changed to be some ..._ci collation, then the ORDER BY will do what you want without any special syntax in the query, itself.
See the reference manual on "collation".
PS: Changing the column definition to a suitable collation is more efficient than LOWER or the COLLATE clause, especially for large tables.
Use utf8_unicode_ci or utf8mb4_0900_ai_ci in your case. I suggest utf8mb4_0900_ai_ci because it has more characters.
SELECT * FROM <table> ORDER BY <column> COLLATE utf8mb4_0900_ai_ci;
If you have no reason to choose a schema collation, select utf8mb4_0900_ai_ci
does anyone has explaination why :
SELECT * FROM MY_TABLE WHERE 1 = 1 AND libelle COLLATE latin1_general_ci LIKE '%dég%'
returns 1 record (only the record with é) while
SELECT * FROM MY_TABLE WHERE 1 = 1 AND libelle COLLATE latin1_swedish_ci LIKE '%dég%'
returns 4 record (of course including the one above) ?
According to MySQL doc latin1_general_ci is "Multilingual (Western European) case insensitive" so should not it manage accents like latin1_swedish_ci ?
Thanks
Nicolas
I suspect you're misunderstanding what collation is.
Collation is a set of rules used in natural language (Swedish, English, Russian, Japanese...) to determine the dictionary order of words. In relational databases, this is used to sort data (e.g. ORDER BY clauses) and to compare data (e.g. WHERE clauses or unique indexes). A couple of examples:
If you need to order by country name in English you get this:
Canada
China
Colombia
However, in traditional Spanish ch used to be an independent letter so correct order was this:
Canada
Colombia
China
In Swedish, å is a separate letter so you can have a login name like ångström even if you already have angström. In other languages they'd be duplicates and would not be allowed.
Collation is not something you use to display emojis and other Unicode characters. That's just encoding (ISO-8859-1, UTF-8, UTF-16... whatever).
I want to check if a string consists only of uppercase letters. I know that RLIKE/REGEXP are not case sensitive in MySQL, so I tried to use the :upper: character class:
SELECT 'z' REGEXP '^[[:upper:]]+$';
This gives true, although the z is in lower case. Why is that?
REGEXP is not case sensitive, except when used with binary strings.
http://dev.mysql.com/doc/refman/5.7/en/regexp.html
So with that in mind, just do something like this:
SELECT * FROM `users` WHERE `email` REGEXP BINARY '[A-Z]';
Using the above example, you'd get a list of emails that contain one or more uppercase letters.
For me this works and is not using a regexp. It basically compares the field with itself uppercased by mysql itself.
-- will detect all names that are not in uppercase
SELECT
name, UPPER(name)
FROM table
WHERE
BINARY name <> BINARY UPPER(name)
;
change to case sensitive collation, eg.
CHARACTER SET latin1 COLLATE latin1_general_cs
then try this query,
SELECT 'z' REGEXP '^[A-Z]+$'
SQLFiddle Demo
The most voted answer doesn't work for me, I get the error:
Character set 'utf8mb4_unicode_ci' cannot be used in conjunction with 'binary' in call to regexp_like.
I used the MD5 to compare the original value and the lowercased value:
SELECT * FROM user WHERE MD5(email) <> MD5(LOWER(email));
example:
SELECT country
FROM data
WHERE city LIKE
(SELECT LEFT ('jakartada',7));
it's working nicely but if i give delimiter with value:4 ,i must give wildcard
like this ---> "%string%" ,where i can give wildcard in the query?
With LIKE you can use the following two wildcard characters in the pattern.
Character Description
% Matches any number of characters, even zero characters
_ Matches exactly one character
\% Matches one “%” character
\_ Matches one “_” character
does this query help you?
SELECT country
FROM data
WHERE city LIKE CONCAT ('%', (SELECT LEFT ('jakartada',7)), '%');
why you using sub query?
SELECT * FROM `data` WHERE city LIKE 'jakar%'
I'm having problems searching in multiple tables for different values. When I search for "paul" I get nothing, but if I search for "Paul" I get the corresponding persons for orders with the name Paul.
$get_orders = mysql_query("
SELECT
co.id, co.final_id, co.shop_id, co.customer_id, co.payment_type, co.payment_currency, co.billing_email, co.billing_first_name, co.billing_last_name, co.delivery_first_name, co.delivery_last_name, UNIX_TIMESTAMP(co.order_created) AS order_created, c.email, s.site_name,
MATCH(co.final_id, co.billing_first_name, co.billing_last_name, co.delivery_first_name, co.delivery_last_name, co.order_created)
AGAINST ('$match_against' IN BOOLEAN MODE) AS score
FROM customer_orders AS co
LEFT JOIN customers AS c ON c.id = co.customer_id
LEFT JOIN shops AS s ON s.id = co.shop_id WHERE co.status = '{$os}'
ORDER BY score DESC
LIMIT $offset,$views_page") or die(mysql_error());
I've search all over for a solution to this problem. I've tried using UPPER, changing the database collation from utf8_general_ci to utf8_bin (binary) but my problem still remains unsolved..
All suggestions are appreciated..
Regards
from the mysql manual:
The default character set and collation are latin1 and
latin1_swedish_ci, so nonbinary string comparisons are case
insensitive by default. This means that if you search with col_name
LIKE 'a%', you get all column values that start with A or a. To make
this search case sensitive, make sure that one of the operands has a
case sensitive or binary collation. For example, if you are comparing
a column and a string that both have the latin1 character set, you can
use the COLLATE operator to cause either operand to have the
latin1_general_cs or latin1_bin collation:
see: mysql case sensitivity
See http://bugs.mysql.com/bug.php?id=22343
From my understanding, make sure everything is a string if you want to search for a string.
Also, switch charset back to case-insensitive. No need for it to be binary.