获取varchar字段的最大记录

Getting max record on varchar field

我有这个问题

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)

Content 是一个 varchar 字段,我需要获取最新记录。我现在明白 MAX 不会按照我想要的方式在 varchar 字段上工作。我不确定如何使用 MAX idgroup 获取相应的内容25=] 在此连接中。

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

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 
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

您必须在 max(created)join 以提供最新内容。

或者您可以将查询更改为

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

as mysql 即使您没有在 group by 子句中包含所有非聚合列,也允许您这样做。但是,除非您在最大日期 join,否则您不会得到正确的结果。

这里有两个选择。如果您的 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).

上的索引

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

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;