Using Columns in a RegExp in MySQL

后端 未结 1 488
情深已故
情深已故 2020-12-30 09:37

I\'m using the following query with regexp:

SELECT a.id, a.company, a.name, b.title, b.description, b.t_id
FROM a, b
WHERE ( b.title
REGEXP \"[[         


        
相关标签:
1条回答
  • 2020-12-30 10:02

    You're searching for the literal string a.company, and not the column. Try this:

    SELECT a.id, a.company, a.name, b.title, b.description, b.t_id
    FROM a, b
    WHERE 
        ( 
            b.title REGEXP concat('[[:<:]]', a.company, '[[:>:]]') 
            OR b.description REGEXP concat('[[:<:]]', a.company, '[[:>:]]') 
            OR b.title REGEXP concat('[[:<:]]', a.name, '[[:>:]]')
            OR b.description REGEXP concat('[[:<:]]', a.name, '[[:>:]]')
        ) 
        AND a.company !=  '' AND a.name !=  ''
    

    This provides the regexp with the value of the column, not the string 'a.company'. Since my guess is that you want to compare the column value (and not the column name), you will need to concatenate your regexp together.

    You can test this with this query:

    select
        'My col: a.company' as Test1,
        'My col: ' + a.company as Test2
    from
        a
    

    Here, Test1 will always be the value My col: a.company, while Test2 will be My col: <company col value here>.

    0 讨论(0)
提交回复
热议问题