Jump to content

How to run multiple conditions on one field


smash120

Recommended Posts

I am having a very difficult time trying to get my query to work correctly and at the moment it does not do what I need it to do. Here is what I want to do

SELECT account_num from patient_info where dob='1901' OR dob ='1912' OR dob ='1940'

Than loop throu the result set and run another query like so

foreach ($accounts as $account) {
$q=$this->db->query("SELECT account_num
FROM (
SELECT account_num, CONCAT(prescription,'-',dosage) as script
FROM ci_prescription_history
)a
WHERE script='".implode("' OR script ='",$doses)."' AND account_num=$account");
}

Then run another query similar to the one above. But this does not work for me because it will grab any row which matches either of the OR statements when I need it to match both. So I tried running this query but it returns an empty set.

$foreach ($accounts as $account) {
q=$this->db->query("SELECT account_num
FROM (
SELECT account_num, CONCAT(prescription,'-',dosage) as script
FROM ci_prescription_history
)a
WHERE script='".implode("' AND script ='",$doses)."' AND account_num=$account");
}

I also tried using the IN clause but thats the same as the OR statement. How can I accomplish this query? I am still learning advance queries so please bare with me.

Link to comment
Share on other sites

You should NEVER run queries in loops. You need to learn how to use JOINs.

 

For example, the looping query can be replaced with this (i.e. no need for the first query)

$query = "SELECT history.account_num, CONCAT(history.prescription,'-',history.dosage) as script
          FROM ci_prescription_history as history
          JOIN patient_info as patient
            ON patient.account_num = history.account_num
          WHERE history.script IN ({$scripts})
            AND patient.dob IN ('1901', '1912', '1940')";

Note; this isn't tested since I don't have your database, but it should be close.

Edited by Psycho
Link to comment
Share on other sites

  • 2 weeks later...

You should NEVER run queries in loops. You need to learn how to use JOINs.

 

For example, the looping query can be replaced with this (i.e. no need for the first query)

$query = "SELECT history.account_num, CONCAT(history.prescription,'-',history.dosage) as script
          FROM ci_prescription_history as history
          JOIN patient_info as patient
            ON patient.account_num = history.account_num
          WHERE history.script IN ({$scripts})
            AND patient.dob IN ('1901', '1912', '1940')";

Note; this isn't tested since I don't have your database, but it should be close.

I tired using your query but I cant get it to work. I get this error "Unknown column 'history.script' in 'where clause'". I also tried rewriting it with the subquery but I still cant get it to work. Im not that great with databases so Im pretty sure it's something I am doing.

Link to comment
Share on other sites

You can not use aliases in the WHERE clause, so

 

WHERE CONCAT(history.prescription,'-',history.dosage) IN ('$scripts');

Thanks for that info so here is what I have so far but I am getting an empty result set

SELECT history.account_num, CONCAT(history.prescription,'-',history.dosage) as script, patient.gender
FROM ci_prescription_history as history
JOIN ci_patient_info as patient
ON patient.account_num = history.account_num
WHERE patient.gender="male" 
AND CONCAT(history.prescription,'-',history.dosage) IN ($scripts)
HAVING COUNT(script)=$scripts

If I remove the having count clause I will get a list but I only want the records that have the right count. So in this case the prescriptions would be Flonase-50 mcg/inh and Singulair-5mg so count is 2 and I should get three records but I don't get any.

Edited by smash120
Link to comment
Share on other sites

What does scripts contain? It is being used as a list of strings and as a count in the query

I wrote the query wrong when I added the code here but in my project it is written correctly.  It should read like this

SELECT history.account_num, CONCAT(history.prescription,'-',history.dosage) as script, patient.gender
FROM ci_prescription_history as history
JOIN ci_patient_info as patient
ON patient.account_num = history.account_num
WHERE patient.gender="male" 
AND CONCAT(history.prescription,'-',history.dosage) IN ($scripts)
HAVING COUNT(script)=count($prescriptions[0])

so $scripts is a string and $prescriptions[0] is an array

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.