Very Odd WHERE Clause Issue!

7 posts by 3 authors in: Forums > CMS Builder
Last Post: April 30, 2011   (RSS)

By Perchpole - April 28, 2011

Over the last few days I've been struggling with a WHERE clause issue that I just couldn't get right. I've finally made the thing work but the solution seems completely about-face!

I am trying to return a list of records. Each record has a multi-select list field which allows you to assign it to various sub-groupings.

The URL of the viewer page is appeneded with &sub=n - where n is the value of the sub-group.

The idea is that when the page is displayed, it will show only those records assigned to the particular sub-group. Sounds simple...

All I need to do is use the following code strings to construct the WHERE clause:

$selectedSubGroupNum = @$_REQUEST['sub'];

$record['subGroup']


I assumed the WHERE clause would look like this...

list($records, $recordsMetaData) = getRecords(array(
'tableName' => 'table',
'where' => " '%\t{subGroup}\t%' IN ($selectedSubGroupNum) ",
));


...but it didn't work - and I couldn't understand why?

In the end (after almost 12 hours of head-banging!) I tried this...

list($records, $recordsMetaData) = getRecords(array(
'tableName' => 'table',
'where' => " subGroup LIKE '%\t{$selectedSubGroupNum}\t%' ",
));


...and it worked - but I have no idea why?! It seems completely against everything I've learned (here)!

subGroup returns a tab-delimited list of numbers - therefore surely it should be used in this form %\t{subGroup}\t% - but that didn't work.

Only when I'd swapped the clause around did the viewer page return the correct records. Can someone please explain why?!

:o/

Perch

Re: [Perchpole] Very Odd WHERE Clause Issue!

By robin - April 28, 2011

Hey Perch,

Your original where statement didn't work for a couple reasons. Wildcards like '%' don't work with IN. Also subGroup is the entire column, so it can't be IN a single value.

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

Wildcards do work with with LIKE though so a single value with wildcards can match to the column.

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like

Hope that helps,
Robin
Robin
Programmer
interactivetools.com

Re: [robin] Very Odd WHERE Clause Issue!

By Perchpole - April 28, 2011

Hi, Robin -

Thanks for the feedback. My mind is a little clearer but I still can't seem to grasp the fundamental mechanics of the clause.

For me...

'where' => " subGroup LIKE '%'$selectedSubGroupNum}%' ",

...seems to be the wrong way round.

Does the argument work either way?

Perhaps I would understand things a little better if there were some way of illustrating the mechanics schematically?

:o/

Perch

Re: [Perchpole] Very Odd WHERE Clause Issue!

By Jason - April 28, 2011

Hi Perch,

Using the LIKE statement with the % wildcards, you'll need to have the query like this:

'where' => " subGroup LIKE '%$selectedSubGroupNum%'",

If it helps, think of it like telling the database to go through each record and check the value of "subGroup" to see if the value stored in $selectedSubGroupNum appears somewhere in the string. If it does, return that record.

Also, if the field subGroup is a multi-value field, you'll need to add \t to your query like this:

'where' => " subGroup LIKE '%\t$selectedSubGroupNum\t%'",

Hope this helps clarify
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Very Odd WHERE Clause Issue!

By Perchpole - April 28, 2011

Hi, Jason -

Thanks. I do understand the basics - it's just the bit about the tab-delimited string which I'm finding tricky.

subGroup returns a tab-delimited list of numbers - and yet the \t codes are applied around $selectedSubGroupNum which is just a single number.

It seems the wrong way around!

Wibble...

Perch

Re: [Jason] Very Odd WHERE Clause Issue!

By Perchpole - April 30, 2011

Jason -

Got it!

I had always thought the \t code meant "ignore" tabs - which is perhaps why I tried to construct my clause the wrong way around.

Thanks for explaining things!

:0)

Perch