Forum Moderators: coopster

Message Too Old, No Replies

Query Multiple Variables

I need a practical way to do a pseudo "AND/OR" statement

         

jjhenry78

7:57 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



I have figured out how to pass variables from dynamically generated form menus on my site. The variables are passing correctly, however, i need a way to query the results to refine by which variables get passed. My current mySQL code is as follows:

SELECT tblgroup.groupID, tblgroup.lastName, tblgroup.typeID, tblgroup.ageRangeID, tblgroup.timeID FROM tblgroup WHERE
(tblgroup.typeID = %s AND tblgroup.ageRangeID = %s AND tblgroup.timeID = %s)

I also had it set up with: .

"SELECT tblgroup.groupID, tblgroup.lastName, tblgroup.typeID, tblgroup.ageRangeID, tblgroup.timeID FROM tblgroup WHERE
(tblgroup.typeID = %s OR tblgroup.ageRangeID = %s OR tblgroup.timeID = %s)"

the results that get returned show all groups for all of the first variable that gets passed. I need a way to check for the first variable, then move to the second, then the third, if they are used. To me, it seems i need something like an "AND/OR" statement but don't think that is valid SQL. Can anyone advise?

rockerzz501

9:01 am on Dec 29, 2008 (gmt 0)

10+ Year Member



Are you using PHP with mysql? If yes then it will not be that hard.YOu can use PHP script to interact with mysql database.

For example..the process will be;
1. 1st query
2. get the query results and process it in PHP
3. forward the PHP processing results to 2nd mysql query (2nd variable)

Do this until all variables are processed. This makes your SQL query and php scripts looks very simple and easy to troubleshoot in case there are issues. You can even output the result using php echo in each step to see if that is exactly what you have expected.

jjhenry78

3:05 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



Thanks, that sounds reasonable. Could you give me a practical example of how that code would look? I understand how to develop one SQL query, but dont quite get how I would "re-query" that result through another query. I'm not sure how to call the results of the the previous query into the next.

it seems i would do something like:

SELECT * from TABLE WHERE condition = var 1, but don't see how i would get that result into another query.

sorry i am a noob at this. can you advise a bit further?

JonW

6:06 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



This isn't PHP, though it might help:

select="SELECT tblgroup.groupID, tblgroup.lastName, tblgroup.typeID, tblgroup.ageRangeID, tblgroup.timeID FROM tblgroup WHERE ("

if get("typeid")!="" then where=" AND tblgroup.typeID=" + get("typeid")

if get("ageRangeID")!="" then where=where +" AND tblgroup.ageRangeID=" + get("tblgroup.ageRangeID")

if get("timeID")!="" then where=where +" AND tblgroup.timeID=" + get("tblgroup.timeID")

/trim the first 'and' and add a closing paren.
query=select + substring(where,5) + ')'

jjhenry78

6:20 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



Maybe i was supposed to read more into that or program better, but the result of that code gave me an error. Perhaps i didn't write out the if/then in the SQL statement correctly.

SELECT *
FROM tblgroup
WHERE ("if varType !="" then WHERE = "tblgroup.typeID= varType" if varAge != "" then WHERE tblgroup.ageRangeID = varAge AND tblgroup.typeID = varType" if varTime != "" then WHERE tblgroup.timeID = varTime AND tblgroup.ageRangeID = varAge AND tblgroup.typeID = varType")

any advice?