INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Querying multiple checkboxes in different states

Querying multiple checkboxes in different states

(OP)
Hi All,

I have a table with three checkboxes that I want to query. I need to be able to check any box (or boxes) and return records where the box/boxes is checked, but not records where it is unchecked.

I tried this but it keeps returning all records, regardless of the checked status:

CODE --> SQL

(chk1 LIKE %s OR %s IS NULL) OR (chk2 LIKE %s OR %s IS NULL) OR (chk3 LIKE %s OR %s IS NULL) 


If I do the following, it works fine when 1 box is checked but as soon as I check more than 1 box I get no records returned.

CODE --> SQL

(chk1 LIKE %s OR %s IS NULL) AND (chk2 LIKE %s OR %s IS NULL) AND (chk3 LIKE %s OR %s IS NULL) 

What would be the proper way to write out this query so that the proper records are returned?

Thanks in advance for any help.

Kind regards,

Ken

RE: Querying multiple checkboxes in different states

assuming the value of a checked box is 1 then to retrieve records where box 1 is checked

CODE

select * from table
where box1=1 

but i suspect that is not what you want.
the best solution would be to improve the code that generates your sql so that it tests the incoming value and determines whether or not that part of the where clause should be there.

failing that, assuming that values are numeric, why not do something like the following

CODE

WHERE SUM(chk1) = %s
      OR SUM(chk2) = %s
      OR SUM(chk3) = %s
GROUP BY {the primary key} 
and the %s should be 1 or 0 depending on whether you are trying to select one or more of the checkboxes positively.

RE: Querying multiple checkboxes in different states

hi,

A CHECKBOX is either CHECKED (TRUE) or NOT (FALSE)

CODE

Where (
      chk1 
  OR  chk2 
  OR  chk3
      ) 
so if any of the three is TRUE then the criteria is TRUE

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Querying multiple checkboxes in different states

I don't really understand. Checkboxes are gui elements how do they pertain to your query? if chk1, chk2,chk3 contain the values of your checkboxes what is in your %s strings?

Can you explain a bit more what you are attempting to do?

You might want to provide your table structure also.










----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Querying multiple checkboxes in different states

In MS Access, for instance, there is a field data type of yes/no

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Querying multiple checkboxes in different states

this is what the OP wants (I thought)

1. a form will preselect between 1 and 3 checkboxes
2. some script will then assemble a query
3. if chk1 is checked in 1 then the OP wants all records which have chk1 checked
4. ditto chk2 and 3
5. all in the same script (thus dynamic query compilation).

easy to do if we knew the language. as a generic solution I think that mine worked although perhaps this would be better to take account of possible empty strings or NULl values being passed

CODE

WHERE SUM(chk1) >= IFNULL(CONVERT(%s, UNSIGNED),0)
      OR SUM(chk2) >= IFNULL(CONVERT(%s, UNSIGNED),0)
      OR SUM(chk3) >= IFNULL(CONVERT(%s, UNSIGNED),0)
GROUP BY {the primary key} 

i'm not sure about whether the comparison should be >= or = as I am not totally sure of the use case either!

RE: Querying multiple checkboxes in different states

This produces what seem to be the intended results in MA Access...

CODE

SELECT Distinct name1
FROM Table1
WHERE (ck1 or ck2 or ck3); 


ID	ck1	ck2	ck3	name1
1	Yes	No	No	skip
2	No	No	Yes	mary
3	No	No	No	fred
 

results.
name1
skip
mary
 

Skip,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

RE: Querying multiple checkboxes in different states

(OP)
Thanks Guys for all your replys!
Skip I see where it works in Access I am not sure how to translate that to MySQL though. This is the intended result, however. I am wanting to return records where any of the check boxes are checked. In my table, the values stored for the check boxes are 0=unchecked and -1=checked. I am treating these as text for the purposes of the query and the %s is the way Adobe Dreamweaver holds the value of the variables. It's a function called GetSQLValueString(), which escapes the string using MySQL's built-in string escaping function, then if it is a non-numeric value, surrounding it in single quotes. This function was written for inserting variable data into SQL queries.

Hope this helps.

Ken

RE: Querying multiple checkboxes in different states

My code should work fine for that. Set each %s to 1

Or just add the three fields together (chk1+chk2 etc) and test for less than 0. Make sure that you are not passing strings in that case.

Don't use the built in Dreamweaver functions. They are not good quality.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close