![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL automatic string to integer casting in WHERE clause to fetch a specific id
If the string begins with integer then it converts the string to integer, otherwise it won’t. Let us first create a −
mysql> create table DemoTable1390 -> ( -> StudentId varchar(20) -> ); Query OK, 0 rows affected (0.93 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1390 values('563_John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1390 values('1001_Carol_Taylor'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1390 values('David_Miller_789'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable1390 values('456_AdamSmith'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select −
mysql> select * from DemoTable1390;
This will produce the following output −
+-------------------+ | StudentId | +-------------------+ | 563_John | | 1001_Carol_Taylor | | David_Miller_789 | | 456_AdamSmith | +-------------------+ 4 rows in set (0.00 sec)
Following is the query for automatic string to integer casting in where clause to fetch a specific −
mysql> select * from DemoTable1390 where StudentId=456;
This will produce the following output −
+---------------+ | StudentId | +---------------+ | 456_AdamSmith | +---------------+ 1 row in set, 4 warnings (0.02 sec)
Advertisements