MySQL Forums
Forum List  »  Performance

Re: Problem with MySQL SELECT
Posted by: Rick James
Date: April 22, 2011 09:56AM

mysql> SELECT 'A'=1, 'A'=0, 'A'=1234;
+-------+-------+----------+
| 'A'=1 | 'A'=0 | 'A'=1234 |
+-------+-------+----------+
|     0 |     1 |        0 |
+-------+-------+----------+
To explain...

'A' is a string.
0, 1, 1234 are numbers.
'=', in this syntax, is a comparison operator, not an assignment operator.
'=' compares a number with a string as a numeric comparison.
The string is converted to a number. If there are leading digits in the string, those will be converted. If not, then it will be treated as 0.
mysql> SELECT 0=1, 0=0, 0=1234;
+-----+-----+--------+
| 0=1 | 0=0 | 0=1234 |
+-----+-----+--------+
|   0 |   1 |      0 |
+-----+-----+--------+
To explain further...
'=' returns a TRUE/FALSE value.
TRUE is a synonym for 1.
FALSE is a synonym for 0.

If you want to assign a "variable", then use this syntax:
mysql> SELECT @A := 12;
+----------+
| @A := 12 |
+----------+
|       12 |
+----------+

mysql> SELECT @A * @A;
+---------+
| @A * @A |
+---------+
|     144 |
+---------+

Another tidbit of info can be found in the WARNINGS:
mysql> SELECT 'A'=1, 'A'=0, 'A'=1234, '1234xyz'=1234;
+-------+-------+----------+----------------+
| 'A'=1 | 'A'=0 | 'A'=1234 | '1234xyz'=1234 |
+-------+-------+----------+----------------+
|     0 |     1 |        0 |              1 |
+-------+-------+----------+----------------+
1 row in set, 4 warnings (0.01 sec)

mysql> SHOW WARNINGS;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'A'       |
| Warning | 1292 | Truncated incorrect DOUBLE value: 'A'       |
| Warning | 1292 | Truncated incorrect DOUBLE value: 'A'       |
| Warning | 1292 | Truncated incorrect DOUBLE value: '1234xyz' |
+---------+------+---------------------------------------------+

Options: ReplyQuote


Subject
Views
Written By
Posted
2170
April 20, 2011 02:55AM
Re: Problem with MySQL SELECT
736
April 22, 2011 09:56AM
700
May 02, 2011 04:36AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.