1

编辑:

我正在尝试做一个 MySQL 查询,它将为我提供重复项的最新条目以及没有任何重复项的条目。

这是我的桌子的样子:

导师表:

   mentor_id    applicant_id    mentor_count    mento_email mentor_name mentor_pass
    193     92      1       test@yahoo.com  test        1234
    194     92      2       table@yahoo.com table       4567
    195     92      3       lamp@yahoo.com  lamp        7890
    196     92      1       test@yahoo.com  test        1234
    197     92      2       table@yahoo.com table       4567
    198     92      3       lamp@yahoo.com  lamp        7890

MF表:

  mf_id     mentor_id   dept        contact     orgname     yrs     length      sak social  char    goal    resp    emomat  res others  impact  evaluation
43      193     math dept   9111111     etc     1       1       e   e   e   e   e   e   e   e   e   good
114     196     math dept   9111111     etc     1       1       e   e   e   e   e   e   e   e   e   good
193     197     sci dept    9222222     org     2       2       n   n   n   n   n   n   n   n   n   medium
194     194     sci dept    9222222     org     2       2       n   n   n   n   n   n   n   n   n   medium
220     195     eng dept    9333333     hello       3       3       q   q   q   q   q   q   q   q   q   bad

我尝试使用此查询:

 SELECT *
FROM mentor m1
LEFT JOIN (

SELECT mentor_name, max( mentor_id ) AS maxid
FROM mentor m
GROUP BY m.mentor_id
)m2 ON m1.mentor_name = m2.mentor_name
AND m1.mentor_id < m2.maxid
LEFT JOIN mf ON m1.mentor_id = mf.mentor_id
WHERE m1.applicant_id =833
AND m2.maxid IS NULL
ORDER BY m1.mentor_id ASC
LIMIT 0 , 30 

但这就是发生的事情:

mentor_id   applicant_id    mentor_count    mentor_email    mentor_name mentor_pass mentor_name maxid   mf_id   mentor_id   dept    contact orgname yrs length  sak social  char    goal    resp    emomat  res others  spirit  concept comm    impact  evaluation
/*there is data here but the column for mentor_name onwards is null*/

我怎样才能使导师名以后的列不为空,但仍显示最新的重复项以及没有任何重复项的列?

4

2 回答 2

0

我猜你想添加mentor.application_id = mf.application_idJOIN条件

select * 
from mentor 
inner join
(
    SELECT *, max(mentor_id) as maxid 
    from mf 
    group by mentor_id
) mf on mentor.mentor_id = mf.maxid AND mentor.application_id = mf.application_id
where applicant_id = 92

通常,您需要一个额外的条件来获取重复项。除非在同一张表中WHERE applicant_id = 92有其他相同的人,否则不会重复。applicant_id

于 2012-10-01T12:56:38.733 回答
0

尝试

select * from mentor 
where mentor_id in 
(
      SELECT max(mentor_id) from mf 
      where applicant_id = 92 
      group by mentor_id
)
于 2012-10-01T12:54:05.720 回答