我得到重复的数据行,如何解决这个问题?

时间:2011-12-13 07:23:25

标签: mysql database

这是我的示例sql代码

SELECT * FROM wsrecruitcvhead AS CV, wsrecruittimes AS WT 
WHERE
CV.`ResumeTitle` LIKE '%search%'
OR ResumeSummaryIntroduction LIKE '%search%'
OR WT.`AvailableToWorkSunday` = 0 
OR WT.`AvailableToWorkMonday` = 0
OR WT.`AvailableToWorkTuesday` = 0 
OR WT.`AvailableToWorkWednesday` = 0 
OR WT.`AvailableToWorkThursday` = 0
OR WT.`AvailableToWorkFriday` = 0  
AND CV.`ResumeID` = WT.`ResumeID` 
ORDER BY CV.ResumeID ASC;

我正在获取重复的数据行...当我尝试SELECT DISTINCT *时,它没有帮助,该怎么办?,如何解决?

2 个答案:

答案 0 :(得分:1)

您的查询等同于:

SELECT *
  FROM wsrecruitcvhead AS CV, wsrecruittimes AS WT
 WHERE CV.`ResumeTitle` LIKE '%search%'
    OR ResumeSummaryIntroduction LIKE '%search%'
    OR WT.`AvailableToWorkSunday` = 0
    OR WT.`AvailableToWorkMonday` = 0
    OR WT.`AvailableToWorkTuesday` = 0
    OR WT.`AvailableToWorkWednesday` = 0
    OR WT.`AvailableToWorkThursday` = 0
    OR (WT.`AvailableToWorkFriday` = 0
        AND CV.`ResumeID` = WT.`ResumeID`)
 ORDER BY CV.ResumeID ASC;

这意味着您在两个表之间没有良好的连接。学习使用显式连接符号来拯救自己的悲伤:

SELECT *
  FROM wsrecruitcvhead AS CV
  JOIN wsrecruittimes AS WT
    ON CV.`ResumeID` = WT.`ResumeID`
 WHERE CV.`ResumeTitle` LIKE '%search%'
    OR ResumeSummaryIntroduction LIKE '%search%'
    OR WT.`AvailableToWorkSunday` = 0
    OR WT.`AvailableToWorkMonday` = 0
    OR WT.`AvailableToWorkTuesday` = 0
    OR WT.`AvailableToWorkWednesday` = 0
    OR WT.`AvailableToWorkThursday` = 0
    OR WT.`AvailableToWorkFriday` = 0
 ORDER BY CV.ResumeID ASC;

但是,虽然这是对查询的简单直接修改,但您可能会想到:

SELECT *
  FROM wsrecruitcvhead AS CV
  JOIN wsrecruittimes AS WT
    ON CV.`ResumeID` = WT.`ResumeID`
 WHERE (CV.`ResumeTitle` LIKE '%search%'
        OR ResumeSummaryIntroduction LIKE '%search%'
       )
   AND (   WT.`AvailableToWorkSunday`    = 0
        OR WT.`AvailableToWorkMonday`    = 0
        OR WT.`AvailableToWorkTuesday`   = 0
        OR WT.`AvailableToWorkWednesday` = 0
        OR WT.`AvailableToWorkThursday`  = 0
        OR WT.`AvailableToWorkFriday`    = 0
       )
 ORDER BY CV.ResumeID ASC;

甚至(因为没有明显的理由可以省略星期六):

SELECT *
  FROM wsrecruitcvhead AS CV
  JOIN wsrecruittimes AS WT
    ON CV.`ResumeID` = WT.`ResumeID`
 WHERE (CV.`ResumeTitle` LIKE '%search%'
        OR ResumeSummaryIntroduction LIKE '%search%'
       )
   AND (   WT.`AvailableToWorkSunday`    = 0
        OR WT.`AvailableToWorkMonday`    = 0
        OR WT.`AvailableToWorkTuesday`   = 0
        OR WT.`AvailableToWorkWednesday` = 0
        OR WT.`AvailableToWorkThursday`  = 0
        OR WT.`AvailableToWorkFriday`    = 0
        OR WT.`AvailableToWorkSaturday`  = 0
       )
 ORDER BY CV.ResumeID ASC;

答案 1 :(得分:0)

DISTINCT仅在行完全重复时才有效。尝试更具选择性的SELECT,因此请选择您需要的实际列,而不是使用*

另请查看GROUP BYhttp://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

此查询比您的查询要好一些,并且可以更轻松地使用:

    SELECT * 
      FROM wsrecruitcvhead AS CV
INNER JOIN wsrecruittimes  AS WT ON (CV.`ResumeID` = WT.`ResumeID`) 
     WHERE (   CV.`ResumeTitle` LIKE '%search%'
            OR ResumeSummaryIntroduction LIKE '%search%'
            ) 
       AND (   WT.`AvailableToWorkSunday` = 0 
            OR WT.`AvailableToWorkMonday` = 0
            OR WT.`AvailableToWorkTuesday` = 0 
            OR WT.`AvailableToWorkWednesday` = 0 
            OR WT.`AvailableToWorkThursday` = 0
            OR WT.`AvailableToWorkFriday` = 0  
           )
  ORDER BY CV.ResumeID ASC;