how mysql ordering strings? - mysql

SELECT IF('y' = 'i', 1, 2 ) -> 1 why?
Can I change encoding or somethint to get it right? and how to order strings like irish and yes
now field and table encoded in utf8_lithuanian_ci
so how to order list with these characters?

You can compare/order those strings using BINARY operator -
SELECT * FROM table ORDER BY BINARY column;
From the reference - The BINARY operator casts the string following it to a binary string. This is an easy way to force a column comparison to be done byte by byte rather than character by character.

Alphabetic sort is performed with respect of the collation, so you have to find which is better for you.
http://dev.mysql.com/doc/refman/5.0/en/charset-general.html

Ordering works the same for strings as it does for integers. It performs an alphabetic sort.
SELECT * FROM table ORDER BY column ASC

Related

MySQL Regexp on blob column

I am having problem with using MySQL's REGEXP operator with blob column.
Clause:
WHERE table.blobColumn REGEXP '"postalCode";s:[1-9]'
won't return me single result, even I got thousands of records that got 'postal' inside their blob column value. Is there any way I can do REGEXP on this column types in MySQL?
Using a REGEXP on a blob field does not work well because of the ASCII NUL value ('\0').
One way to make it work is to replace the NUL value before doing the REGEXP evaluation, as following:
... WHERE replace(table.blobColumn, '\0', '') REGEXP '"postalCode";s:[1-9]'

Find MySQL DB rows with a match in a pipe delimited column

I'm querying a table that has a column with member_ids stuffed in a pipe delimited string. I need to return all rows where there is an 'exact' match for a specific member_id. How do I deal with other IDs in the string which might match 'part' of my ID?
I might have some rows as follows:
1|34|11|23
1011
23|1
5|1|36
64|23
If I want to return all rows with the member_id '1' (row 1, 3 and 4) is that possible without having to extract all rows and explode the column to check if any of the items in the resulting array match.
MySQL's regular expressions support a metacharacter class that matches word boundaries:
SELECT ...
FROM mytable
WHERE member_ids REGEXP '[[:<:]]1[[:>:]]'
See http://dev.mysql.com/doc/refman/5.6/en/regexp.html
If you don't like that, you can search using a simpler regular expression, but you have to escape the pipes because they have special meaning to regular expressions. And you also have to escape the escaping backslashes so you get literal backslashes through to the regular expression parser.
SELECT ...
FROM mytable
WHERE member_ids REGEXP '\\|1\\|'
You can do this in one expression if you modify your strings to include a delimiter at the start and the end of the string. Then you don't have to add special cases for beginning of string and end of string.
Note this is bound to do a table-scan. There's no way to index a regular expression match in MySQL. I agree with #MichaelBerkowski, you would be better off storing the member id's in a subordinate table, one id per row. Then you could search and sort and all sorts of other things that the pipe-delimited string makes awkward, inefficient, or impossible. See also my answer to Is storing a delimited list in a database column really that bad?
'|' has a specific meaning in REGEXP. So suppose that the ids are separated by another delimiter like '~'.
Then you can run this code:
SELECT * FROM `t1`
where (Address Regexp '^1~') or
(Address Regexp '~1$') or
(Address Regexp '^1$') or
(Address Regexp '~1~')

Mysql sorting of alphanumeric values without considering alphabet

In database, it store values are
M2345
45
M345
E21
A3
is there a way to sort it correctly? like
A3
E21
45
M345
M2345
Assuming there can be at most one letter before the digits start, you could use a condition like this in your sorting definition:
ORDER BY CAST(IF(col REGEXP '^[a-z]', SUBSTRING(col, 2), col) AS SIGNED)
Unfortunately, MySQL doesn't have a replace function that can handle regular expressions, otherwise that would have been very helpful at this point.
You may also want to consider storing the numeric value itself in a separate calculated field for more efficient sorting.

Limit characters for mysql_query(select) lookup

Is there an efficient way to limit the number of characters for a mysql query lookup? So, if a text column field had 18000 characters, but I only need to query 500 characters, is there a way to mysql_query("select text_column from random_table limit_char_count 500")?
How about using the SUBSTRING() function?
Below example selects 4 characters starting from 1st position
SELECT SUBSTRING(text_column,1,4)
FROM random_table
WHERE something = something else
EDIT - edited based on - for all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1.

MySQL: Selecting rows ordered by character count

In MySQL, how can I order my query by character count?
Try using the LENGTH function:
SELECT * FROM table ORDER BY LENGTH(myField);
Depending on what you're doing, you might want to use CHAR_LENGTH instead:
A multi-byte character counts as a single character. This means that for a string containing five two-byte characters, LENGTH() returns 10, whereas CHAR_LENGTH() returns 5.
If you don't know what that means, you probably want LENGTH.