Mysql 分组方式:从历史记录表中检索最后一个事件
我有3个表,我想从历史记录表中检索最后一个事件,所以我写了这个请求,但结果不好。还是我错了?多谢各位Mysql 分组方式:从历史记录表中检索最后一个事件,mysql,date,join,group-by,greatest-n-per-group,Mysql,Date,Join,Group By,Greatest N Per Group,我有3个表,我想从历史记录表中检索最后一个事件,所以我写了这个请求,但结果不好。还是我错了?多谢各位 CREATE TABLE `equipment` ( `id_equipment` int(10) UNSIGNED NOT NULL, `reference` varchar(32) DEFAULT NULL COMMENT 'clé AIRO2', `buying_date` date DEFAULT NULL, `first_use`
CREATE TABLE `equipment` (
`id_equipment` int(10) UNSIGNED NOT NULL,
`reference` varchar(32) DEFAULT NULL COMMENT 'clé AIRO2',
`buying_date` date DEFAULT NULL,
`first_use` date DEFAULT NULL,
`checking_delai` varchar(45) DEFAULT NULL,
`serial_number` varchar(256) DEFAULT NULL,
`lot_number` varchar(256) DEFAULT NULL,
`lapsing_date` date NOT NULL,
`status` varchar(32) NOT NULL,
`inspection` date NOT NULL,
`compteur` int(11) DEFAULT NULL,
`id_model` int(10) UNSIGNED DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`detruit` int(1) DEFAULT NULL,
`date_REEPREUVE` date DEFAULT NULL,
`validite_apragaz` date DEFAULT NULL,
`restituee_AJR` int(11) NOT NULL,
`perdu` int(11) DEFAULT NULL,
`maintenance_catt_o2` date DEFAULT NULL,
`vendu` int(11) DEFAULT NULL,
`colonne_inogen` date DEFAULT NULL COMMENT 'date changement colonne inogen',
`rappel_525KS_dec19` int(1) DEFAULT NULL COMMENT 'rappel_cable_DEVILBISS_525_KS'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='matériel';
CREATE TABLE `equipment_history` (
`id_equipment_history` int(10) UNSIGNED NOT NULL,
`id_user` int(10) UNSIGNED NOT NULL,
`id_equipment` int(10) UNSIGNED DEFAULT NULL,
`id_patient` int(11) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
`status_date` datetime DEFAULT NULL,
`updated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`tuyau` tinyint(1) DEFAULT NULL,
`cable_alim` tinyint(1) DEFAULT NULL,
`compteur` int(11) DEFAULT NULL,
`verif_alarme` tinyint(1) DEFAULT NULL,
`verif_volume` tinyint(1) DEFAULT NULL,
`verif_pression` tinyint(1) DEFAULT NULL,
`verif_o2` tinyint(1) DEFAULT NULL,
`verif_debit` tinyint(1) DEFAULT NULL,
`remplacer_consommable` tinyint(1) DEFAULT NULL,
`complet` tinyint(1) DEFAULT NULL,
`comment_panne` text,
`ras` tinyint(1) DEFAULT NULL,
`panne` tinyint(1) DEFAULT NULL,
`id_piece_jointe` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='historique de l''utilisation du matériel';
CREATE TABLE `patient_has_equipment` (
`id_patient_has_equipment` int(10) UNSIGNED NOT NULL,
`id_intervention` int(10) UNSIGNED DEFAULT NULL,
`id_equipment` int(10) UNSIGNED NOT NULL,
`id_patient` int(10) UNSIGNED DEFAULT NULL,
`date_attribution` date DEFAULT NULL,
`id_user` int(11) DEFAULT NULL,
`version_appli` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
请求:
SELECT
equipment.id_equipment,
equipment.reference,
history.updated,
history.`status`
FROM
equipment
LEFT JOIN
(
SELECT
*
FROM
`equipment_history`
GROUP BY
id_equipment
ORDER BY
`equipment_history`.`updated`
DESC
) AS history
ON
history.id_equipment = equipment.id_equipment
WHERE
history.`status` = 3 AND equipment.id_equipment NOT IN(
SELECT
id_equipment
FROM
patient_has_equipment
)
谢谢问题出在
分组依据
子查询上。虽然这在大多数数据库中是一个语法错误,但不幸的是,MySQL容忍这种语法(当禁用sql模式时,仅\u FULL\u GROUP\u BY
时),但并没有做您认为的事情:实际上,您从表中为每个设备id
创建了一条任意记录(order BY
子句对这种行为没有影响)
您需要筛选上一个历史记录,而不是聚合。我认为这应该满足你的要求:
select
e.id_equipment,
e.reference,
eh.updated,
eh.status
from equipment e
inner join equipment_history eh on eh.id_equipment = e.id_equipment
where
eh.status = 3
and eh.updated = (
select max(eh1.updated)
from equipment_history eh1
where eh1.id_equipment = e.id_equipment
)
and not exists (
select 1
from patient_has_equipment phe
where phe.id_equipment = e.id_equipment
)
groupby
子查询就是问题所在。虽然这在大多数数据库中是一个语法错误,但不幸的是,MySQL容忍这种语法(当禁用sql模式时,仅\u FULL\u GROUP\u BY
时),但并没有做您认为的事情:实际上,您从表中为每个设备id
创建了一条任意记录(order BY
子句对这种行为没有影响)
您需要筛选上一个历史记录,而不是聚合。我认为这应该满足你的要求:
select
e.id_equipment,
e.reference,
eh.updated,
eh.status
from equipment e
inner join equipment_history eh on eh.id_equipment = e.id_equipment
where
eh.status = 3
and eh.updated = (
select max(eh1.updated)
from equipment_history eh1
where eh1.id_equipment = e.id_equipment
)
and not exists (
select 1
from patient_has_equipment phe
where phe.id_equipment = e.id_equipment
)
我删除了sqlserver标记,因为语法表明您正在运行mysql。请一次只标记一个数据库。我删除了sqlserver标记,因为语法表明您正在运行mysql。请一次只标记一个数据库。