最大功能无效,返回最小功能值

时间:2019-07-02 01:52:17

标签: mysql mysql-workbench

max函数在我的存储过程中不起作用,它与min函数返回相同的值,但在sql编辑器中有效。我的专栏是int无符号的。这是我的代码:

下面的这些代码可以正常工作,但不能在我的存储过程中使用。

select idPatient from tb_patient where nomPatient='CARRIE' and prenomPatient='Marie Claude' and idPatient>267; -- result 268
select max(idPatient) from tb_patient where nomPatient='CARRIE' and prenomPatient='Marie Claude'; -- result 268
select idPatient from tb_patient where nomPatient='CARRIE' and prenomPatient='Marie Claude'; -- result 267 268

drop procedure if exists findTwins;
delimiter ##
create procedure findTwins()
begin 
    declare getNom varchar(40);
    declare getPrenom varchar(40);
    declare getId int default 1;
    declare getId2 int default 1;
    if(select count(*) from tb_patient group by nomPatient,prenomPatient having count(*)=2 limit 1)
    then
        select nomPatient,prenomPatient into getNom,getPrenom from tb_patient group by nomPatient,prenomPatient having count(*)=2 limit 1; 
        set getId=(select min(idPatient) from tb_patient where nomPatient=getNom and prenomPatient=getPrenom);
        set getId2=(select  max(idPatient) from tb_patient where nomPatient=getNom and prenomPatient=getPrenom);
        select concat(getNom,' ',getPrenom,' ',getId,' ',getId2) as Patient;
    end if; 
end##

call findTwins();

此查询的结果应为: CARRIE Marie Claude 267 268 但我得到了 CARRIE Marie Claude 267 267 无论我做什么,即使我只运行select ... where ...,如果没有最小或最大函数,我都只能从267中从存储过程中找到268。 表结构。

CREATE TABLE tb_patient (
    idPatient INT UNSIGNED AUTO_INCREMENT PRIMARY KEY ,
    nomPatient varchar(40) not null,
    prenomPatient varchar(40) not null,
    emailPatient varchar(40) unique, 
    index ind_patient_name(nomPatient,prenomPatient))
ngine=innoDB;

0 个答案:

没有答案
相关问题