Suraj Hazarika
Suraj Hazarika

Reputation: 667

Match a value from array of values in mysql

I have a query like this :

$sql = "
        SELECT
            *
        FROM
            tbl_emp_data
        WHERE
            company_qualification = '$q'
    ";

Now my company_qualification field is not a single value ,instead its a words separated by commas like BSc,BA,BCom etc . And the value $q is a single value say BA . So how can I search the company data from the value $q . I can not use LIKE since its going to match fields like BA and BBA .

Upvotes: 0

Views: 127

Answers (3)

OptimusCrime
OptimusCrime

Reputation: 14863

$sql = "
        SELECT
            *
        FROM
            tbl_emp_data
        WHERE
            company_qualification IN ('".implode("','",$q)."')
    ";

Upvotes: 1

Lucas
Lucas

Reputation: 10646

You can use IN like this:

SELECT * FROM tbl_emp_data WHERE company_qualification IN ('BSc', 'BA', 'BCom');

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86346

You can use FIND_IN_SET

$sql = "
        SELECT
            *
        FROM
            tbl_emp_data
        WHERE        
           FIND_IN_SET($q,company_qualification)
    ";

Upvotes: 4

Related Questions