MySQL Escape Character Issue - mysql

While I was trying to solve This Question. I created the dummy records in a table
create table mytable(data CHAR(30));
INSERT INTO mytable VALUES('d\\one'),('d\\two'),('d\\three');
SELECT * FROM mytable;
+---------+
| data |
+---------+
| d\one |
| d\two |
| d\three |
+---------+
3 rows in set (0.00 sec)
Now when i am selecting records, I am getting no result, I have tried many combination with like but no luck.
Ex :
SELECT * FROM mytable WHERE data LIKE "d\\%";
Empty set (0.00 sec)
SELECT * FROM mytable WHERE data LIKE 'd\\%';
Empty set (0.00 sec)

Use triple slash:
SELECT * FROM mytable WHERE data LIKE "d\\\%"

Or use INSTR() instead
SELECT * FROM mytable WHERE instr(data, 'd\\') = 1

Related

can a mysql select query in C language return a field with the special characters escaped?

1- string = a'b"c\d
2- escaped_string = a\'b\"c\\d
3- make an insert query that inserts escaped_string in some table field.
4- make a select query that returns the inserted value.
The returned value is: a'b"c\d
Is there a way to get the select query to return a\'b\"c\\d ?
(I understand that i can escape it again).
You can use the QUOTE() function of mysql:
mysql> select data from x;
+---------+
| data |
+---------+
| a'b"c\d |
+---------+
1 row in set (0.00 sec)
mysql> select quote(data) from x;
+-------------+
| quote(data) |
+-------------+
| 'a\'b"c\\d' |
+-------------+
1 row in set (0.00 sec)
This should exactly do what you are looking for. Note that the " doesn't need to be escaped here, so QUOTE() doesn't escape it, too.

SELECT rows NOT ending in a specific character

I have a table of questions. I need to find rows which have '?' in the question text because of bad character encoding/collation. I need to find all the rows which have '?' but also need to ignore the question marks at the end of the questions. I tried this query but I still get rows with questions marks at end of the question
SELECT *
FROM `kc_questions`
WHERE `question` LIKE "%?%" /* WHICH CONTAINS '?' */
AND `question` NOT LIKE "%?" /* DOES NOT END WITH '?' */
EDIT: phpmyadmin actually tells me there is something wrong with the query:
The query however runs successfully returning rows which end with'?'.
Based on the sample data I tried the following demo and it works as expected.
SQL:
create table kc_questions(question varchar(200));
insert into kc_questions values
('Ex1. ?-particles are harmul for human body. Select True or False.'),
('Ex2. What is your name?');
SELECT question FROM kc_questions;
SELECT *
FROM `kc_questions`
WHERE `question` LIKE "%?%"
AND `question` NOT LIKE "%?";
Output:
mysql> SELECT question FROM kc_questions;
+-------------------------------------------------------------------+
| question |
+-------------------------------------------------------------------+
| Ex1. ?-particles are harmul for human body. Select True or False. |
| Ex2. What is your name? |
+-------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> SELECT *
-> FROM `kc_questions`
-> WHERE `question` LIKE "%?%"
-> AND `question` NOT LIKE "%?";
+-------------------------------------------------------------------+
| question |
+-------------------------------------------------------------------+
| Ex1. ?-particles are harmul for human body. Select True or False. |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.8-rc |
+-----------+
1 row in set (0.00 sec)
You could use a regular expression
SELECT *
FROM `kc_questions`
WHERE `question` REGEXP '.*\?.+$'
Basicly you search for questions which contains '?' with at least one character after the '?'

MySQL SELECT returns empty set when there is data

mysql> SELECT title FROM pages WHERE id=111;
+------------+
| title |
+------------+
| 'Theology' |
+------------+
1 row in set (0.00 sec)
mysql> SELECT id FROM pages WHERE title='Theology';
Empty set (0.00 sec)
The results conflicted. I can't understand that.
Change
'Theology'
to
'\'Theology\''
Seems that the data stored is 'Theology' instead of Theology. Thanks to Abhik Chakraborty.
Use query like this . You need to escape the '
1st Way
SELECT id FROM pages WHERE title = '\'Theology\''
2nd Way
SELECT id FROM pages WHERE title = "'Theology'"
3rd Way
SELECT id FROM pages WHERE title='''Theology''';

How to extract the rows that contain only 1 "=" by using REGEXP in MySQL?

Suppose I have a table named "t1" which contains a column named "ID" which has the following records
abcde=1=2
qwert=3
hhhhj=9
zxcv=5=8
How can I extract the records that contain only 1 "=" sign by using REGEXP in MySQL?
I've tried
SELECT * FROM t1 WHERE ID REGEXP '\\w*=\\w*=\\w*'; -- returns no records
SELECT * FROM t1 WHERE ID REGEXP '\\w*=\\w*'; -- returns all 4 records
I expected the first query to return the records which contains 2 "=", and the second query to return the records which contains only 1 "=".
What's wrong with my queries?
A pretty simple solution without REGEXP would be
select * from table
where
length(ID) - length(replace(ID,'=','')) = 1 ;
Some test cases
mysql> select length('qwert=3') - length(replace('qwert=3','=','')) as diff;
+------+
| diff |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> select length('zxcv=5=8') - length(replace('zxcv=5=8','=','')) as diff;
+------+
| diff |
+------+
| 2 |
+------+
1 row in set (0.00 sec)
SELECT * FROM t1 WHERE ID REGEXP '^[^=]*=[^=]*$';
Guess this should do it.See fiddle http://www.sqlfiddle.com/#!9/b2ead/2/0

mysql wildcards % vs %%

What is the difference in '%' and '%%', when used in mysql where clause with 'LIKE' ?
select * from `wp_users` u where u.user_nicename like "%lastuser%"
VS
select * from `wp_users` u where u.user_nicename like "%%lastuser%%"
There is no difference between %% and % when it comes to pattern matching in mysql.
I've seen developers get confused over this when they try to match a literal % and therefor write %%. This is most often because of the fact that format-strings often use a double % to indicate that you'd like it to be treated as an exact literal.
MySQL documentation of LIKE
MySQL 5.0 Reference Manual :: 11.5.1 String Comparison Functions :: LIKE
What's the origin of the string, and where is it going?
If the string is passed to a function such as sprintf the format-string rule I mentioned earlier is present, though there is no confusion in that case.
The developer want it to be a single % in the string passed to mysql, and therefor wrote %%.
$query = sprintf (
"SELECT ... FROM ... WHERE id <> %d AND data LIKE '%%hello world%%'",
50
);
// $query => "SELECT ... FROM ... WHERE id <> 50 AND data LIKE '%hello world%'";
A few sample SELECTs using the LIKE operator
mysql> SELECT 'abc' LIKE 'ab%';
+------------------+
| 'abc' LIKE 'ab%' |
+------------------+
| 1 |
+------------------+
1 row in set (0.01 sec)
mysql> SELECT 'abc' LIKE 'ab%%';
+-------------------+
| 'abc' LIKE 'ab%%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'abc' LIKE 'ab\%';
+-------------------+
| 'abc' LIKE 'ab\%' |
+-------------------+
| 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT 'ab%' LIKE 'ab\%';
+-------------------+
| 'ab%' LIKE 'ab\%' |
+-------------------+
| 1 |
+-------------------+
1 row in set (0.00 sec)