我的数据库中有 8 个表,我需要在一次查询中获取所有必要的数据。
当不使用 where
语句时,我的查询正常工作,但是当我在查询中使用 LIKE
和 OR LIKE
时,where
语句无法正常工作
我原来的查询是:
SELECT `mhr_patients`.`id`,
`mhr_patients`.`fullName`,
`mhr_patients`.`nationalCode`,
`mhr_patients`.`age`,
`mhr_patients`.`firstGCS`,
`mhr_patients`.`fileNumber`,
`mhr_patients`.`isUnKnown`,
`mhr_patients`.`docDetail`,
`mhr_patients`.`presentation`,
`mhr_patients`.`appRegisterTime`,
`mhr_patients`.`inspectorRegisterTime`,
`mhr_patients`.`patientStatusDetail`,
`mhr_patients`.`patientDetail`,
`mhr_patients`.`status`,
`docT`.`text` AS docText,
`tolOp`.`name` AS tolOpName,
`tolOp`.`color` AS tolOpColor,
`tolOp`.`res1` AS tolOpTextColor,
`pLog`.`breathing`,
`pLog`.`bodyMovement`,
`pLog`.`faceMovement`,
`pLog`.`gag`,
`pLog`.`cough`,
`pLog`.`cornea`,
`pLog`.`pupil`,
`pLog`.`dollEye`,
`pLog`.`secondGCS`,
`pLog`.`sedation`,
`pLog`.`inspector`,
`pLog`.`status` As pLogStatus,
`pLog`.`section`,
`pLog`.`id` AS pLogId,
`pLog`.`typeOfSection`,
`pLog`.`lastUpdateTime`,
`pLog`.`isTransfer`,
`pLog`.`opu`,
`hos`.`name` AS hosName,
`mhr_opu`.`name` AS opuName,
`mhr_opu`.`id` AS opuId,
`mhr_states`.`name` AS cityName,
`mhr_inspectors`.`name` AS insName
FROM (`mhr_patients`)
JOIN `mhr_doc` AS docT ON `docT`.`id` = `mhr_patients`.`doc`
JOIN `mhr_tol_options` AS tolOp ON `tolOp`.`id` = `mhr_patients`.`patientStatus`
JOIN `mhr_patients_log` AS pLog ON `pLog`.`pId` = `mhr_patients`.`id` AND pLog.id = (SELECT MAX(mhr_patients_log.id) FROM mhr_patients_log WHERE mhr_patients_log.pId = mhr_patients.id)
JOIN `mhr_hospitals` AS hos ON `hos`.`id` = `pLog`.`hospital`
JOIN `mhr_opu` ON `mhr_opu`.`id` = `pLog`.`opu`
JOIN `mhr_states` ON `mhr_states`.`id` = `pLog`.`city`
JOIN `mhr_inspectors` ON `mhr_inspectors`.`id` = `pLog`.`inspector`
WHERE
`mhr_patients`.`status` = 5
GROUP BY `pLog`.`pId`
ORDER BY `mhr_patients`.`inspectorRegisterTime` asc
LIMIT 30
当我使用上面的查询时,结果是正确的,当我更改 mhr_病人
.status
时,返回不同的结果,
但是当我使用以下查询 mhr_ Patients
.status
时,它不起作用,无论参数是什么,都会给出一个结果
SELECT `mhr_patients`.`id`,
`mhr_patients`.`fullName`,
`mhr_patients`.`nationalCode`,
`mhr_patients`.`age`,
`mhr_patients`.`firstGCS`,
`mhr_patients`.`fileNumber`,
`mhr_patients`.`isUnKnown`,
`mhr_patients`.`docDetail`,
`mhr_patients`.`presentation`,
`mhr_patients`.`appRegisterTime`,
`mhr_patients`.`inspectorRegisterTime`,
`mhr_patients`.`patientStatusDetail`,
`mhr_patients`.`patientDetail`,
`mhr_patients`.`status`,
`docT`.`text` AS docText,
`tolOp`.`name` AS tolOpName,
`tolOp`.`color` AS tolOpColor,
`tolOp`.`res1` AS tolOpTextColor,
`pLog`.`breathing`,
`pLog`.`bodyMovement`,
`pLog`.`faceMovement`,
`pLog`.`gag`,
`pLog`.`cough`,
`pLog`.`cornea`,
`pLog`.`pupil`,
`pLog`.`dollEye`,
`pLog`.`secondGCS`,
`pLog`.`sedation`,
`pLog`.`inspector`,
`pLog`.`status` As pLogStatus,
`pLog`.`section`,
`pLog`.`id` AS pLogId,
`pLog`.`typeOfSection`,
`pLog`.`lastUpdateTime`,
`pLog`.`isTransfer`,
`pLog`.`opu`,
`hos`.`name` AS hosName,
`mhr_opu`.`name` AS opuName,
`mhr_opu`.`id` AS opuId,
`mhr_states`.`name` AS cityName,
`mhr_inspectors`.`name` AS insName
FROM (`mhr_patients`)
JOIN `mhr_doc` AS docT ON `docT`.`id` = `mhr_patients`.`doc`
JOIN `mhr_tol_options` AS tolOp ON `tolOp`.`id` = `mhr_patients`.`patientStatus`
JOIN `mhr_patients_log` AS pLog ON `pLog`.`pId` = `mhr_patients`.`id` AND pLog.id = (SELECT MAX(mhr_patients_log.id) FROM mhr_patients_log WHERE mhr_patients_log.pId = mhr_patients.id)
JOIN `mhr_hospitals` AS hos ON `hos`.`id` = `pLog`.`hospital`
JOIN `mhr_opu` ON `mhr_opu`.`id` = `pLog`.`opu`
JOIN `mhr_states` ON `mhr_states`.`id` = `pLog`.`city`
JOIN `mhr_inspectors` ON `mhr_inspectors`.`id` = `pLog`.`inspector`
WHERE
`mhr_patients`.`status` = 5
AND `mhr_patients`.`fullName` LIKE '%aaa%'
OR `mhr_patients`.`nationalCode` LIKE '%aaa%'
OR `mhr_patients`.`fileNumber` LIKE '%aaa%'
OR `mhr_patients`.`age` LIKE 'aaa'
OR `mhr_patients`.`firstGCS` LIKE 'aaa'
OR `mhr_patients`.`patientDetail` LIKE '%aaa%'
GROUP BY `pLog`.`pId`
ORDER BY `mhr_patients`.`inspectorRegisterTime` asc
LIMIT 30
问题是什么?
最佳答案
您在查询中混合了 and
和 or
。这基本上会否定你的 where 语句。您需要使用括号来对逻辑进行分组
WHERE
`mhr_patients`.`status` = 5
AND
(`mhr_patients`.`fullName` LIKE '%aaa%'
OR `mhr_patients`.`nationalCode` LIKE '%aaa%'
OR `mhr_patients`.`fileNumber` LIKE '%aaa%'
OR `mhr_patients`.`age` LIKE 'aaa'
OR `mhr_patients`.`firstGCS` LIKE 'aaa'
OR `mhr_patients`.`patientDetail` LIKE '%aaa%' )
GROUP BY `pLog`.`pId`
ORDER BY `mhr_patients`.`inspectorRegisterTime` asc
LIMIT 30
您可能希望所有患者的状态均为 5,并且具有全名(如 %aaaa%)或国家代码(如 %aaa$ 等)。要实现此目的,您需要使用括号将您的 或 语句组合成一个单位。
关于php - 在我的 mysql 查询中无法正常工作的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27628199/