简体   繁体   中英

Mysql query advice/help needed

I am building a site where candidates can post their resume and employers can post their jobs.

The employers can post a job with multiple qualifications and I saved it in database like this PHP,Javascript,ASP . Now I want admin to be able to select the candidates who are eligible for a post.

I have written the query:

$sql = "
        SELECT
            cand_f_name,
            cand_l_name,
            cand_qualification,
            cand_phone,
            cand_email,
            cand_experience_yr,
            cand_experience_mn,
            cand_message,
            cand_id
        FROM
            tbl_cand_data
        WHERE
            cand_qualification LIKE '%$emp_q%' 

But it is not showing the expected result. I think my condition cand_qualification LIKE '$emp_q' is wrong.

My tbl_cand_data : 在此处输入图片说明

If you are doing a LIKE query you should include wildcards, as they will match a string containing, otherwise just do an exact match using =:

// String match
WHERE
    cand_qualification LIKE '%emp_q%';

// Exact match
WHERE
    cand_qualification = '$emp_q';

// You could try a WHERE IN clause as well
WHERE cand_qualification IN ('$emp_q');

// Values have to be quoted individually
WHERE cand_qualification IN ('BA','BSc','BCom');

// If you have an array you can do this:    
$myArray = array('BA', 'BSc', 'BCom');
$emp_q = "'" . implode("','", $myArray) . "'"; //Output 'BA', 'BSc', 'BCom'

I saved it in database like this PHP,Javascript,ASP

That's what you did utterly wrong.

you have to create a related table (that's why our ratabase called relational one!) storing qualifications, and interconnection table, consists of qualifications id linked with candidates ids.
And query them using basic joins.

Note that despite of your current decision, even if you decide to continue with your lame schema, you WILL have to remake it proper way, sooner or later (but sooner will make you less work).
That is the very basics of database architecture and noone can go against it.

SELECT fields FROM tbl_cand_data d, qualification q, qual_cand qc 
WHERE q.name = 'ASP' AND q.id=qc.qid AND d.id=qc.did

喜欢,需要%%尝试一下

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM