Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Querying multiple checkboxes in different states

Status
Not open for further replies.

deeciple

Technical User
Mar 1, 2012
70
US
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:
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.
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
 
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.

 
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,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
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
 
In MS Access, for instance, there is a field data type of yes/no

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
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!
 
This produces what seem to be the intended results in MA Access...
Code:
SELECT Distinct name1
FROM Table1
WHERE (ck1 or ck2 or ck3);

[pre]
ID ck1 ck2 ck3 name1

1 Yes No No skip
2 No No Yes mary
3 No No No fred
[/pre]

results.
[pre]
name1
skip
mary
[/pre]


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top