"SELECT" selects wrong rows when umlauts are present in table - mysql

The structure of table 'textconstraint' (collation utf8_general_ci) is as follows:
+-----+---------+
| id | pattern |
+-----+---------+
| 11 | Ä |
| 27 | A |
+-----+---------+
When I query
SELECT * FROM textconstraint WHERE pattern = 'A' LIMIT 1;
The following rows are selected
+----+---------+
| id | pattern |
+----+---------+
| 11 | Ä |
+----+---------+
Why A-umlaut is selected instead of A?
P.S. I do SET NAMES UTF8

You can try this:
SELECT *
FROM textconstraint
WHERE pattern = BINARY 'A'
See this fiddle.

Related

SQL - Select column with certain value in it and other random values

Let me say i have a table called test with the following data
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
| 5 | 864296100119956 |
What i want to do is to be able to write a select statement that returns a 3 rows with two random values and one mandatory value from the t_number column
for example if the mandatory value is 864291100247345 the output should something like below
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 4 | 864296100098325 |
OR
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
I have tried the below query but it's not yielding the output i expect, in a sense that it does return a result but without the mandatory value
SELECT * FROM test WHERE t_number = 864291100247345 OR id LIMIT 3;
What is the best way to go about this?
Thank you.
You can use order by:
SELECT t.*
FROM test
ORDER BY (t_number = 864291100247345) DESC,
rand()
LIMIT 3;
This returns the mandatory number first and then random numbers after that.
MySQL treats boolean values (the result of the = expression) as numbers in a numeric context, with "1" for true and "0" for false. So the first expression in the order by sorts the result set with the "true" conditions first, followed by the others.

mysql regexp two words enclosed with any special characters

+-----+------+
| A | B |
+-----+------+
| gan | esh |
| dhi | nesh |
+-----+------+
I have a table like this. I want to check this with another table has column
+----------------+
| C |
+----------------+
| !!dhin!!esh |
| gan!!esh.. |
| $$$gan%%%esh.. |
+----------------+
The following query works fine
select * from table1 a, table2 b where c like concat('%',a,'%',b'%')
but what I want is starting, ending, middle should contain only non-alphanumeric. I am a newbie to regexp.
CONCAT("[^[:alnum:]]", a, "[^[:alnum:]]+", b, "[^[:alnum:]]")
Or perhaps
CONCAT("\\b", a, "[^[:alnum:]]+", b, "\\b")

INNER JOIN same value, but the difference is the other table are having extra word in front of the value

As I said in the title, or maybe my question is a little bit confusing. Here it is....
So, I want to combine 2 tables using INNER JOIN (ofcourse) with some difference.
This is my tables
Table 1, PK = steam_id
SELECT * FROM nmrihstats ORDER BY points DESC LIMIT 4;
+---------------------+----------------+--------+-------+--------+
| steam_id | name | points | kills | deaths |
+---------------------+----------------+--------+-------+--------+
| STEAM_0:1:88467338 | Alan14 | 50974 | 5438 | 12 |
| STEAM_0:0:95189481 | ? BlacKEaTeR ? | 35085 | 24047 | 316 |
| STEAM_0:1:79891668 | Lowell | 34410 | 44076 | 993 |
| STEAM_0:1:170948255 | Rain | 29780 | 30167 | 278 |
+---------------------+----------------+--------+-------+--------+
4 rows in set (0.01 sec)
Table 2, PK = authid
SELECT * FROM store_players ORDER BY credits DESC LIMIT 4;
+-----+-------------+---------------+---------+--------------+-------------------+
| id | authid | name | credits | date_of_join | date_of_last_join |
+-----+-------------+---------------+---------+--------------+-------------------+
| 309 | 1:88467338 | Alan14 | 15543 | 1475580801 | 1482260232 |
| 368 | 1:79891668 | Lowell | 10855 | 1475603908 | 1482253619 |
| 256 | 1:128211488 | Fuck[U]seLF | 10422 | 1475570061 | 1482316480 |
| 428 | 1:74910707 | Mightybastard | 7137 | 1475672897 | 1482209608 |
+-----+-------------+---------------+---------+--------------+-------------------+
4 rows in set (0.00 sec)
Now, how can I use INNER JOIN without doing like removing "STEAM_0:" or adding it. Also with explanation, please
You can join witn like operator, e.g.:
SELECT n.*, sp.*
FROM nmrihstats n JOIN store_players sp ON n.steam_id LIKE CONCAT('%', sp.authid);
Here's the SQL Fiddle.
Another approach would be to use String functions of MySQL to extract out relevant part from steam_id but I believe that's not what you want:
SELECT SUBSTR(steam_id, LOCATE('STEAM_0:', steam_id) + CHAR_LENGTH('STEAM_0:'))
FROM nmrihstats;
it is not possible, you need to remove "STEAM_0:", matching with WHERE, using substring for remove STEAM_0: from column equals to column in other table, or a new field into the T1 without "STEAM_0:", that 2 columns match for INNER JOIN

How to replace all special characters and spaces in a table column?

I have a table named "contacts" with below fields
id | phone_mobile
1 | 9988011223
2 | 00-12-123456
3 | 91-8876980987
4 | (91) 88990099777
I want a select query which will return below output
id | phone_mobile
1 | 9988011223
2 | 0012123456
3 | 918876980987
4 | 9188990099777
For a known set of characters you can use replace() function chaining something as
mysql> select replace(replace(replace('00-12-123456','-',''),'(',''),')','');
+----------------------------------------------------------------+
| replace(replace(replace('00-12-123456','-',''),'(',''),')','') |
+----------------------------------------------------------------+
| 0012123456 |
+----------------------------------------------------------------+
So in your case it could be
select
id,
replace(replace(replace(replace(phone_mobile,'-',''),'(',''),')',''),' ','') as phone_mobile
from table_name
If there is a long list of characters to be replaced then its better to use the application level to do the job since the chaining of replace becomes really messy.

Best way to find a line in MySQL by piece of value

Database:
+----+------------+
| id | somevalue |
+----+------------+
| 1 | 1000 |
+----+------------+
| 2 | 1001 |
+----+------------+
| 3 | 1002 |
+----+------------+
| 4 | 10021 |
+----+------------+
Question:
What is the best way to find a row by a string which contains piece of "somevalue"?
1. Let's say string is 1002123456. So in this case I must find row with ID 4.
2. Let's say string is 1002345678. So in this case I must find row with ID 3.
Would "MySQL LIKE" work in this scenario?
UPDATE:
Database has 60k rows and "somevalue" should be matched from the front. Because piece of "somevalue" might contain different rows, for example:
+----+------------+
| id | somevalue |
+----+------------+
| 1 | 1000 |
+----+------------+
| 27 | 371000 |
+----+------------+
I am looking for a way to make the process as fast as possible.
SELECT * FROM `db`.`table` WHERE 'someval' LIKE CONCAT('%',`field` , '%') ;
The RLIKE allowes you to match regular expressions in the fields
SELECT * FROM 'table' WHERE 'searchNumber' LIKE CONCAT('%', somevalue, '%')