I am learning Node JS using MySQL as the backend DB in a Linux server and I have encountered something I do not understand and it is all related to MySQL itself.
I have the following table with one row of data. The id of this row is 6
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`authorID` int(11) DEFAULT NULL,
`title` varchar(90) COLLATE utf8_bin DEFAULT NULL,
`body` mediumtext COLLATE utf8_bin,
`createdDate` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
So the following query works fine and returns the one row in the table.
SELECT P.id,U.userName as `authorName`,U.userEmail as `authorEmail`,P.title,P.body,P.createdDate
FROM posts P LEFt JOIN users U ON P.authorID = U.id WHERE P.id = '6'
This other query also works (returns no data):
SELECT P.id,U.userName as `authorName`,U.userEmail as `authorEmail`,P.title,P.body,P.createdDate
FROM posts P LEFt JOIN users U ON P.authorID = U.id WHERE P.id = '60'
But...! The following query is where I am lost (it return the row with ID 6!!!):
SELECT P.id,U.userName as `authorName`,U.userEmail as `authorEmail`,P.title,P.body,P.createdDate
FROM posts P LEFt JOIN users U ON P.authorID = U.id WHERE P.id = '6F'
Why? I am passing a literal 6F inside of single quotes (to make sure) and it should not return anything. There is no post with ID 6F. By the way, I can substitute 6F with 6whatever and the problem will persist. F6 however will not return data as expected.
Why is this? Is it because F is a character and MySQL is expecting an integer and therefore truncate the anything that is not part of the integer?
And more importantly, How can I eliminate this ambiguity?