获取varchar字段的最大记录

时间:2015-08-12 13:12:59

标签: mysql sql join max

我有这个查询

SELECT 
s.account_number,
a.id AS 'ASPIRION ID',
a.patient_first_name,
a.patient_last_name,
s.admission_date,
s.total_charge,
astat.name AS 'STATUS',
astat.definition,
latest_note.content AS 'LAST NOTE',
a.insurance_company
FROM
accounts a
    INNER JOIN
services s ON a.id = s.account_id
    INNER JOIN
facilities f ON f.id = a.facility_id
    INNER JOIN
account_statuses astat ON astat.id = a.account_status_id
    INNER JOIN
(SELECT 
    account_id, MAX(content) content, MAX(created)
FROM
    notes
GROUP BY account_id) latest_note ON latest_note.account_id = a.id
WHERE
    a.facility_id = 56

我的问题来自

(SELECT 
    account_id, MAX(content) content, MAX(created)
FROM
    notes
GROUP BY account_id)

内容是一个varchar字段,我需要获取最新记录。我现在明白MAX不会像我想要的那样在varchar字段上工作。我不确定如何通过此加入中的 MAX ID 组帐户ID 来获取相应的内容。

最好的方法是什么? 我的笔记表看起来像这样......

id  account_id  content         created 
1   1           This is a test  2011-03-16 02:06:40 
2   1           More test       2012-03-16 02:06:40 

3 个答案:

答案 0 :(得分:0)

SELECT 
s.account_number,
a.id AS 'ASPIRION ID',
a.patient_first_name,
a.patient_last_name,
s.admission_date,
s.total_charge,
astat.name AS 'STATUS',
astat.definition,
latest_note.content AS 'LAST NOTE',
a.insurance_company
FROM
accounts a
INNER JOIN services s ON a.id = s.account_id
INNER JOIN facilities f ON f.id = a.facility_id
INNER JOIN account_statuses astat ON astat.id = a.account_status_id
INNER JOIN
(SELECT account_id, MAX(created) mxcreated
 FROM notes GROUP BY account_id) latest_note ON latest_note.account_id = a.id and   
 latest_note.mxcreated = --datetime column from any of the other tables being used
WHERE a.facility_id = 56

您必须join max(created)才会提供最新内容。

或者您可以将查询更改为

SELECT account_id, content, MAX(created) mxcreated
FROM notes GROUP BY account_id

mysql允许您,即使您未在group by子句中包含所有非聚合列。但是,除非您在最长日期join,否则您将无法获得正确的结果。

答案 1 :(得分:0)

这是两个选择。如果您的content不是很长并且没有时髦的字符,您可以使用substring_index() / group_concat()技巧:

(SELECT account_id,
        SUBSTRING_INDEX(GROUP_CONCAT(content ORDER BY created desc SEPARATOR '|' 
                                    ), 1, '|') as content
 FROM notes
 GROUP BY account_id
) latest_note
ON latest_note.account_id = a.id

鉴于列和表的名称,这可能不起作用。然后,您需要from子句中的其他连接或相关子查询。我认为在这种情况下这可能是最简单的:

select . . .,
       (select n.content
        from notes n
        where n.account_id = a.id
        order by created desc
        limit 1
       ) as latest_note
from . . .

此方法的优点是它只获取所需行的注释。并且,您不需要left join来保留所有行。为了提高性能,您需要notes(account_id, created)上的索引。

答案 2 :(得分:0)

最后创建的记录是不存在更新的记录。因此:

SELECT 
  s.account_number,
  a.id AS "ASPIRION ID",
  a.patient_first_name,
  a.patient_last_name,
  s.admission_date,
  s.total_charge,
  astat.name AS "STATUS",
  astat.definition,
  latest_note.content AS "LAST NOTE",
  a.insurance_company
FROM accounts a
INNER JOIN services s ON a.id = s.account_id
INNER JOIN facilities f ON f.id = a.facility_id
INNER JOIN account_statuses astat ON astat.id = a.account_status_id
INNER JOIN
(
  SELECT account_id, content
  FROM notes
  WHERE NOT EXISTS 
  (
    SELECT *
    FROM notes newer
    WHERE newer.account_id = notes.account_id
    AND newer.created > notes.created
  )
) latest_note ON latest_note.account_id = a.id
WHERE a.facility_id = 56;