所以我有一个这样的 SELECT 语句:
SELECT
POSITION(
'^email' IN b.form) + CHAR_LENGTH('^email...') as 'email_start',
LOCATE('~',b.form, POSITION(
'^email' IN b.form) + CHAR_LENGTH('^email...')) as 'email_end',
(LOCATE('~',b.form, POSITION(
'^email' IN b.form) + CHAR_LENGTH('^email...')) -
POSITION(
'^email' IN b.form) + CHAR_LENGTH('^email...')) as 'num_chars',
b.form as 'Form'
FROM
f98_bookingdates as bd
JOIN
f98_booking as b
on b.booking_id = bd.booking_id
LIMIT 20
它返回:
email_start email_end num_chars
444 474 48
email_start是正确的,email_end是正确的。
但是 474 - 444 不是 48...
任何帮助将不胜感激...
我会在计算的第二部分周围添加一个额外的括号:
(LOCATE('~',b.form, POSITION(
'^email' IN b.form) + CHAR_LENGTH('^email...')) -
(POSITION(
'^email' IN b.form) + CHAR_LENGTH('^email...'))) as 'num_chars'
问题是您要先计算起始位置,然后从结束位置中减去它。