0

I am working in codeigniter. I have two table job_post and snapshot.I have written query to fetch data from above table like :

Query1 :

select skill from snapshot where user_id = 1

and it gives and array like :

Array
(
[0] => stdClass Object
    (
        [skill] => 1,2
    )
)

Query2 :

select emp_id,skill from job_post

and this query return array like this :

Array
(
[0] => stdClass Object
    (
        [emp_id] => 10
        [skill] => 1,2,3
    )

[1] => stdClass Object
    (
        [emp_id] => 10
        [skill] => 3,12,13
    )
)

Now, I want to match these to array. If query1 skill match with the query2 skill then its return emp_id. So how can I get that emp_id?

Note : Partical match with query1 and query2

7
  • Full match or partial match? Commented Apr 5, 2016 at 12:11
  • @Sougata partial match Commented Apr 5, 2016 at 12:12
  • Please show us what you have tried in order to achieve this Commented Apr 5, 2016 at 12:13
  • Ok. SO have you tried anything? Commented Apr 5, 2016 at 12:16
  • No, because i dont have any idea about it. Commented Apr 5, 2016 at 12:18

3 Answers 3

1

Store below query's response in a variable lets say in $mySkill.

select skill from snapshot where user_id = 1;

"select emp_id,skill from job_post where concat(',',skill,',') like '%,".$mySkill.",%'"

Alternative: you need to explode $myskill & then need to create or condition dynamically.like:

$arrmyskill = explode(",",$mySkill); 
foreach($arrmyskill as $skill )
{ 
   $str .=" or concat(',',skill,',') like '%,".$skill.",%' ";
} 
$finalqry = "select emp_id,skill from job_post where concat(',',skill,',') like '%".$mySkill."%'" .$str;
Sign up to request clarification or add additional context in comments.

3 Comments

Here I have a problem. it find skill as 1 and 2 in job_post table.If both skill are match then it will return.
Here I have skill as 1 den it match with 11 so its gives wrong result and i remove % then it does not give any output that match with exact 1. so what can i do?
I am very sorry, missed the comma...Please check my edited post
0

Try this code, it will solve your problem

<?php 
$arr1 = array('skill'=> '1,2,3');
$arr2 = array(array('emp_id'=>1,'skill'=> '1,2,3'),array('emp_id'=>1,'skill'=> '1,2,4'));
foreach($arr2 as $key => $val)
{
    if($val['skill']== $arr1['skill'])
    {
        echo 'Matched Id :'.$val['emp_id'];
    }
}
?>

this will output :

Matched Id :1

Comments

0

Why not use single SQL query to JOIN both tables? If any skill(s) matches, the query will give you needed emp_id with matched skills in addition.

SELECT
    j.emp_id,
    GROUP_CONCAT(j.skill) AS skills
FROM
    job_post j
    INNER JOIN `snapshot` s ON s.skill = j.skill
WHERE
    s.user_id = 1
GROUP BY
    j.emp_id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.