繁体   English   中英

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

内容是一个varchar字段,我需要获取最新记录。 我现在知道,MAX无法按照我想要的方式在varchar字段上工作。 我不确定如何在此联接中获取具有最大 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
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最大约会日期,否则您将无法获得正确的结果。

这是两个选择。 如果您的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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM