MySQL LOCATE-POSITION返回错误的数字

时间:2019-04-09 18:04:33

标签: mysql sql

所以我有一个这样的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 ...

任何帮助将不胜感激...

1 个答案:

答案 0 :(得分:1)

我将在计算的第二部分周围添加一个括号:

(LOCATE('~',b.form, POSITION(
            '^email' IN b.form) + CHAR_LENGTH('^email...')) -    
(POSITION(
         '^email' IN b.form) + CHAR_LENGTH('^email...'))) as 'num_chars'

共鸣是您要首先计算起始位置,然后从终止位置减去该起始位置。