Home > OS >  MySQL find if one column contains any values of another column
MySQL find if one column contains any values of another column

Time:05-26

ID||business_name    || district
------------------------
1 ||dhaka bank       || cumilla
------------------------
2 ||cumilla dodhi    || rajshahi
------------------------
3 ||maloncha         || dhaka
------------------------
4 ||dhaka bank       || dhaka

I need the query to find the business name that contains any district

ID||business_name    || district
------------------------
1 ||dhaka bank       || cumilla
------------------------
2 ||cumilla dodhi    || rajshahi
------------------------
4 ||dhaka bank       || dhaka

Your help is greatly appreciated. Thank You.

CodePudding user response:

Use exists logic along with word boundaries:

SELECT ID, business_name, district
FROM yourTable t1
WHERE EXISTS (
    SELECT 1
    FROM yourTable t2
    WHERE t1.business_name REGEXP CONCAT('\\b', t2.district, '\\b')
    -- Note: On MySQL 5.7 and earlier use:
    -- WHERE t1.business_name REGEXP CONCAT('[[:<:]]', t2.district, '[[:>:]]')
);

CodePudding user response:

You Can try this if it helps

Select * from table_name where district IS NOT NULL;

  • Related