我正在尝试从表中进行SELECT,但我需要修剪前导和尾随字符串以进行比较。例如,
我的表/数据非常简单
CREATE TABLE `lcr` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`prefix` varchar(45) DEFAULT NULL,
`digits` varchar(45) DEFAULT NULL,
`price` decimal(20,4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
mysql> select * from lcr;
+----+--------+--------+--------+
| id | prefix | digits | price |
+----+--------+--------+--------+
| 1 | 1122 | 92300 | 0.0198 |
+----+--------+--------+--------+
1 row in set (0.00 sec)
我正在运行以下选择
SELECT * FROM `lcr` WHERE `digits` IN ((SELECT (TRIM(LEADING '1122' FROM '112292300'))), (SELECT (TRIM(LEADING '1122' FROM '11229230'))), (SELECT (TRIM(LEADING '1122' FROM '1122923'))), (SELECT (TRIM(LEADING '1122' FROM '112292'))))
从数字中修剪前导1122,并给出正匹配。但是当我运行查询时
SELECT * FROM `lcr` WHERE `digits` IN ((SELECT (TRIM(LEADING '1122' FROM '92300'))), (SELECT (TRIM(LEADING '1122' FROM '9230'))), (SELECT (TRIM(LEADING '1122' FROM '923'))), (SELECT (TRIM(LEADING '1122' FROM '92'))))
我也得到了积极的一致。因为Trim实际上没有做任何事情,在我的表中,数字是92300.
有没有办法我们可以使用某种条件或函数或类似的东西,只有在前缀+ trim =实际数据传递时才返回结果?
我可能没有太多表达方式在这里提出我的问题,请为此道歉并告诉我是否有任何信息需要回答我的问题。
感谢。
答案 0 :(得分:0)
试试这个
SELECT * FROM `lcr`
WHERE `digits` Like '1122%'
AND `digits` IN (SELECT (TRIM(LEADING '1122' FROM '112292300')
答案 1 :(得分:0)
@ michael-sqlbot必须说,这是一个非常好的和详细的回复。感谢。但是,我们已经做了很多事情来正确运行LCR设置。
回到我的问题,答案要简单得多。
SELECT * FROM `lcr` WHERE `digits` IN ((SELECT (TRIM(LEADING '1122' FROM '92300'))), (SELECT (TRIM(LEADING '1122' FROM '9230'))), (SELECT (TRIM(LEADING '1122' FROM '923'))), (SELECT (TRIM(LEADING '1122' FROM '92'))))
由于我们需要匹配PREFIX + DIGITS,因此解决方案实际上是使用 CONCAT(prefix
,digits
)来修复查询:)< / p>
SELECT * FROM `lcr` WHERE CONCAT (`prefix`, `digits`) IN ((SELECT (TRIM(LEADING '1122' FROM '92300'))), (SELECT (TRIM(LEADING '1122' FROM '9230'))), (SELECT (TRIM(LEADING '1122' FROM '923'))), (SELECT (TRIM(LEADING '1122' FROM '92'))))