如何使用数组中的所有值获取记录

时间:2015-01-22 09:27:20

标签: php mysql pdo

数组中的值是动态的可以或多或少。 如果任何值为真,我会得到结果。 但是如果数组中的所有值都为真,我想获取记录。 重要:数组中的值是动态的

 SELECT J.ID , J.U_POST_ID,
                    J.TITLE,J.CREATION_DATE,J.STATUS,
                    R.FIRST_NAME, R.LAST_NAME,R.CLINICAL_CLINIC_NAME,
                    J.REQUIREMENT,J.STATE,J.CITY,J.DESCRIPTION,J.CALL_DUR,J.USER_ID
                    FROM df_job_meta M JOIN df_job_post J ON 
                    M.JOB_ID = J.ID JOIN df_register_users R ON 
                    R.ID = J.USER_ID WHERE J.STATUS='ACTIVE'  AND R.OCCUPATION !='student' AND  M.VALUE IN ('Fresher','Full Time') group  BY J.ID

表:df_job_meta

--------------------------------------
|JOB_ID  | META_KEY    | VALUE       | 
-------------------------------------|
| 91     | experience  |Fresher      |
| 91     | work_hour   |Full Time    |
| 91     | pay_scale   |Sharing      |
| 91     | gender      |Female       |
| 92     | experience  |3 to 5 Years |
| 92     | work_hour   |Part Time    |  
---------------------------------------

2 个答案:

答案 0 :(得分:1)

我认为您需要的是将返回的记录数与IN子句中的标准数进行匹配。在这种情况下,基本上使用

SELECT M.JOB_ID
FROM df_job_meta M
WHERE M.VALUE IN ('Fresher','Full Time')
GROUP BY M.JOB_ID
HAVING COUNT(DISTINCT M.VALUE) = 2

数组中有 2 个值,因此count必须等于 2 。如果您有 N 值,则查询应该类似于

SELECT M.JOB_ID
FROM df_job_meta M
WHERE M.VALUE IN (1,2,...,N)
GROUP BY M.JOB_ID
HAVING COUNT(DISTINCT M.VALUE) = N

你可以使用php实现该部分,因为你有一个动态数组。当然,您需要使用上面的主要查询加入该查询。

修改

如果每个VALUE没有相同JOB_ID的重复行,则只需执行COUNT(M.VALUE)而不是COUNT(DISTINCT M.VALUE)

编辑2

我还包含了完整的sql语句。 (由于未提供ddl,可能会出错)

SELECT J.ID , J.U_POST_ID,
J.TITLE,J.CREATION_DATE,J.STATUS,
R.FIRST_NAME, R.LAST_NAME,R.CLINICAL_CLINIC_NAME,
J.REQUIREMENT,J.STATE,J.CITY,J.DESCRIPTION,J.CALL_DUR,J.USER_ID
FROM df_job_post J JOIN df_register_users R ON R.ID = J.USER_ID 
WHERE J.STATUS='ACTIVE'  AND R.OCCUPATION !='student' 
AND  J.ID IN 
(
    SELECT M.JOB_ID
    FROM df_job_meta M
    WHERE M.VALUE IN ('Fresher','Full Time')
    GROUP BY M.JOB_ID
    HAVING COUNT(DISTINCT M.VALUE) = 2
) 

编辑3

要进行精确匹配,您可以将查询更改为此

SELECT J.ID , J.U_POST_ID,
J.TITLE,J.CREATION_DATE,J.STATUS,
R.FIRST_NAME, R.LAST_NAME,R.CLINICAL_CLINIC_NAME,
J.REQUIREMENT,J.STATE,J.CITY,J.DESCRIPTION,J.CALL_DUR,J.USER_ID
FROM df_job_post J JOIN df_register_users R ON R.ID = J.USER_ID 
WHERE J.STATUS='ACTIVE'  AND R.OCCUPATION !='student' 
AND  J.ID IN 
(
    SELECT M.JOB_ID
    FROM df_job_meta M
    WHERE M.VALUE IN ('Fresher','Full Time')
    GROUP BY M.JOB_ID
    HAVING COUNT(DISTINCT M.VALUE) = 2 AND COUNT(DISTINCT M.VALUE) = (SELECT COUNT( * ) FROM df_job_meta WHERE JOB_ID = M.JOB_ID)
) 

您基本上检查返回的行数是否等于实际计数:

COUNT(DISTINCT M.VALUE) = (SELECT COUNT( * ) FROM df_job_meta WHERE JOB_ID = M.JOB_ID)

答案 1 :(得分:-1)

如果您使用的是提到的查询,请使用此方法:

$q = mysql_query("SELECT J.ID , J.U_POST_ID,
                J.TITLE,J.CREATION_DATE,J.STATUS,
                R.FIRST_NAME, R.LAST_NAME,R.CLINICAL_CLINIC_NAME,
                J.REQUIREMENT,J.STATE,J.CITY,J.DESCRIPTION,J.CALL_DUR,J.USER_ID
                FROM df_job_meta M JOIN df_job_post J ON 
                M.JOB_ID = J.ID JOIN df_register_users R ON 
                R.ID = J.USER_ID WHERE J.STATUS='ACTIVE'  AND R.OCCUPATION !='student' AND  M.VALUE IN ('Fresher','Full Time') group  BY J.ID");

while($r=mysql_fetch_array($q))
{
   echo $r['JOB_ID'].$r['META_KEY'].$r['VALUE'];
}