select distinct a.PatientID,a.PatientCode,a.PatientSex,a.MobileNo,a.HomePhoneNo,a.UserAge,a.PatientName,a.PatientIDCard, DATE_FORMAT(a.RegistDate,'%Y-%m-%d') as RegistDate,
case when b.usedstartTime is not null and b.UsedEndTime is null then '1'
when b.usedstartTime is not null and b.UsedEndTime is not null then '2'
end as 'usedState'
from mets_v_patient_baseinfo a
left join mets_devices_used_history b on a.patientid = b.PatientID
where (select ifnull(IsDeleted,0) from userpublic_info where UserID = a.PatientID ) = 0
and 1=1
order by PatientID Desc limit 0,15