2014-12-23 46 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_patientsstatus,返回不同的結果, 但是當我使用以下查詢mhr_patientsstatus不起作用,無論參數如何,都會給出一個結果

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 
`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 $等全名要做到這一點,你需要使用括號將你的或陳述集中在一個單元中。

+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