UNION ALL之后的WHERE子句

时间:2016-03-25 16:53:25

标签: mysql sql

我有一个名为“寻求者”的表,其中包含一个人员列表。有一个名为“username”的列,其中包含人员的用户名和名为“resume_status”的列,其值为0或1。

目前,下面的查询根本不会检查“搜索者”表。我希望它只显示“resume_status”列中值为“1”的结果。

注意:“搜索者”表中唯一的常用列和值以及下面现有查询中的值是“用户名”列。我的困惑来自于试图弄清楚如何将下面的查询链接到“搜索者”表。

$query="
    (SELECT username, MATCH(highlight) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_highlights HAVING score>0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(skill,skill_list) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_skills HAVING score >0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(education_title,education_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_education HAVING score >0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(employer_title,employer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_employer HAVING score>0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(volunteer_title,volunteer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_volunteer HAVING score >0 ORDER by score desc)
";

3 个答案:

答案 0 :(得分:1)

使用以下查询进行分类

SELECT A.username, A.score FROM
(
    (SELECT username, MATCH(highlight) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_highlights HAVING score>0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(skill,skill_list) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_skills HAVING score >0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(education_title,education_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_education HAVING score >0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(employer_title,employer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_employer HAVING score>0 ORDER by score desc)
    UNION ALL
    (SELECT username, MATCH(volunteer_title,volunteer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score FROM resume_volunteer HAVING score >0 ORDER by score desc)
) A
LEFT JOIN 
seekers on A.username = seekers.username
WHERE seekers.resume_status = 1

答案 1 :(得分:1)

@ Brijesh的答案很好,但我认为这会更快 - 它还会通过在搜索者表上添加一个索引来改进,我们希望在其他表上使用用户名索引。 ..

SELECT * 
FROM
(
  SELECT a.username, MATCH(a.highlight) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
  FROM resume_highlights a
  JOIN seekers ON a.username = seekers.username and seekers.resume_status = 1
  HAVING score>0 

    UNION ALL

  SELECT b.username, MATCH(b.skill,b.skill_list) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
  FROM resume_skills b
  JOIN seekers ON b.username = seekers.username and seekers.resume_status = 1
  HAVING score >0 

    UNION ALL

  SELECT c.username, MATCH(c.education_title,c.education_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
  FROM resume_education c
  JOIN seekers ON c.username = seekers.username and seekers.resume_status = 1
  HAVING score >0 

    UNION ALL

  SELECT d.username, MATCH(d.employer_title,d.employer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
  FROM resume_employer d
  JOIN seekers ON d.username = seekers.username and seekers.resume_status = 1
  HAVING score>0 

    UNION ALL

  SELECT e.username, MATCH(e.volunteer_title,e.volunteer_organization) AGAINST (\"{$keywords}\" IN BOOLEAN MODE) AS score 
  FROM resume_volunteer e
  JOIN seekers ON e.username = seekers.username and seekers.resume_status = 1
  HAVING score >0 

) AS X
ORDER BY SCORE desc

答案 2 :(得分:1)

您可以使用IN运算符来过滤用户名。
OR
您可以通过别名查询(称为派生表)来使用联接,并与"搜索者"列上的表"用户名",因此您可以很好地使用查询的其他列。