2014-12-23 3 views
0

내 데이터베이스에 8 개의 테이블이 있고 그 중 하나의 쿼리에 필요한 모든 데이터가 있어야합니다. where 문을 사용하지 않을 경우 , 내 쿼리가 작동,하지만 난 내 쿼리 LIKEOR LIKE를 사용할 때 where 문이 제대로내 mysql 쿼리에서 제대로 작동하지 않습니다

내 원래 쿼리를 작동하지 않는 것은 :

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 

무엇이 문제입니까?

답변

1

쿼리에 andor을 혼합합니다. 이것은 기본적으로 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 
당신은 가능성 중 하나 등 %의 단 $ 같은 %의 AAAA %의 또는 nationalCode 같은 이름이 이러한 목표를 달성하기 위해 5의 상태와 모든 환자가 의도

당신에게 귀하 또는 진술을 하나의 단위로 그룹화하려면 괄호를 사용해야합니다.

+0

그것은 작동하고있어, 덕분에 ... 그것은 내 마음에 아니었다. 하지만 약간의 질문, 나는 codeigniter를 사용하고 나는 $ this-> db-> like(); 활성 레코드에서 내 쿼리에 (&)를 어떻게 추가합니까? – mhrezaei

+0

죄송합니다, codeiginiter 모르겠다. 그것은 다른 질문입니다. 질문을 해결 한 경우이를 답변으로 표시하는 것을 잊지 마십시오. – Robbert

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으로 해석합니다.

+0

그것은 고맙습니다 ... 그것은 내 마음에 없었습니다. 하지만 약간의 질문은 codeigniter를 사용하고 $ this-> db-> like(); 활성 레코드에서 내 쿼리에 (&)를 어떻게 추가합니까? – mhrezaei

관련 문제