I have a table with a Binary PK:
CREATE TABLE `codes` (
`Code` binary(45) NOT NULL,
PRIMARY KEY (`Code`),
UNIQUE KEY `Code_UNIQUE` (`Code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
When I insert I use BINARY('value').
I attempted to retrieve a row by Code using the following:
SELECT * from codes WHERE `Code` = BINARY('value')
This doesn't work because the length of the Code field is 45, so MySql pads the data with empty bytes.
This can be visualised running the following:
SELECT HEX(Code), HEX(BINARY('value')) FROM codes
I can get it to work with this (ugly) query:
SELECT * FROM codes WHERE TRIM('0' from HEX(Code)) = HEX(BINARY('value'))
So I was just wondering if anyone can provide a nice and performant way to achieve this. This is straightforward if you know the size of your data, but the Code field can be any length.
