Jump to content

Help with REGEXP or alternative solution sought


lxndr

Recommended Posts

I have a database field that is composed of 16 characters each of which can be set to '0' or '1'.  The characters represent data from a form with 16 checkboxes.  What I want to able to do now is query this field.

 

I was almost able to do it using REGEXP with the following as an example:

 

SELECT * FROM `library` WHERE responses REGEXP '^1......1........$'

 

This works if searching for items that have both the 1st and 8th elements set to 1 but what I really want is for it to search for rows where this field has EITHER 1st or 8th elements set to 1.

 

e.g.

 

If the user does a search and fills out both 1st and 8th checkboxes I'd like the MYSQL query to return the following as an example

 

1000000000000000

0000000100000000

1000000100000000

 

currently it's acting in a logical 'AND' kind of way and only returning:

 

1000000100000000

 

Can anyone tell me the best way to do what I want?

 

TIA

 

 

Link to comment
Share on other sites

sample output and input?

 

I'm not sure what you mean by sample output and input but maybe if I paint one specific example I can apply the proposed solution as a generality.

 

The database field (called responses) is VARCHAR, length 16

 

Example data item:

 

1000000100000000

 

 

I then have an HTML form with 16 checkboxes on, i.e.

 

Item 1

Item 2

 

.

.

.

Item 16

 

Now I want the above data item (1000000100000000)  to be a match if the user selects EITHER item 1 or item 8 and was trying to generate the most elegant SQL solution.  Obviously I can generate multiple OR statements .. ie..

 

SELECT * from table WHERE responses REGEXP '^1...............$' OR responses REGEXP '^.......1........$'

 

what i was hoping is that there's be a neater way of doing it ?

 

 

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.