我的数据库中有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_patients
。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
有什么问题?
答案 0 :(得分:1)
您在查询中混合了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%)或nationalCode(如%aaa $等)。为此,您需要使用括号将您的或语句组合成一个单元。
答案 1 :(得分:0)
您可能打算:
`mhr_patients`.`status` = 5
AND (`mhr_patients`.`fullName` LIKE '%aaa%'
^ -- bracket here
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%' ) <-- close here
你想要status = 5 or a textual match
。没有括号MySQL解释为statsus = 5 or textual match in fullName, or other textual match
。