Querying a string from int column?

前端 未结 4 1873
眼角桃花
眼角桃花 2020-12-20 17:55

I have a table:

CREATE TABLE `ids` (
    id int(11) not null auto_increment,
    PRIMARY KEY (id)
);

It contains some IDs: 111, 112, 113, 1

4条回答
  •  心在旅途
    2020-12-20 18:33

    One option is to CAST the 112 to CHAR to get a proper match:

    WHERE CAST(id AS CHAR(12)) = '112abcdefg'
    

    The 12 in CHAR is a guess; it should be large enough for your biggest id.

    That will probably kill any chance of optimization, so another option (though one I'm not 100% sure of) is to use a BINARY comparison. I've tried this with a few different values and it works:

    WHERE BINARY id = '112abcdefg'
    

提交回复
热议问题