| | #1 |
| The Reality Check War Room Member Join Date: 2006 Location: Cancun, Quintana Roo, MX
Posts: 1,250
Thanks: 610
Thanked 999 Times in 439 Posts
|
I have very little SQL experience and have a problem with a query that seems relatively easy. It's returning a null set and SQL Explain says it's an impossible where. Here's the query: SELECT 'password' FROM `album` WHERE `uid`=124 AND instr('title', '21.01') Here's the DB structure: ![]() Here's the actual record with the matching uid and title: |
| | |
| |
| | #2 |
| White Hat Marketer War Room Member Join Date: 2007 Location: Montgomeryville, PA
Posts: 1,546
Thanks: 77
Thanked 775 Times in 267 Posts
|
INSTR('title', '21.01') looks for the string '21.01' in the string 'title'. That substring does not appear, so the query can never match any rows so none are even checked. What you meant to write was INSTR(title, '21.01') which looks for the string '21.01' in the column named title. I've never seen someone use INSTR though. Most people would write Code: WHERE uid = 124 AND title LIKE '%21.01%' |
| | |
| |
| The Following User Says Thank You to Dan Grossman For This Useful Post: |
| | #3 |
| The Reality Check War Room Member Join Date: 2006 Location: Cancun, Quintana Roo, MX
Posts: 1,250
Thanks: 610
Thanked 999 Times in 439 Posts
|
That works, Dan. Thanks a lot.
|
| | |
| |
| | #4 |
| Social Apps Made Easy War Room Member Join Date: 2012 Location: Mooresville, NC
Posts: 913
Thanks: 231
Thanked 259 Times in 213 Posts
| |
| | |
| |
| | #5 |
| VIP Warrior War Room Member Join Date: 2006 Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
|
For that query, no. In fact it is wrong. instr returns the position of the substring in the main string. |
| |
| | #6 |
| White Hat Marketer War Room Member Join Date: 2007 Location: Montgomeryville, PA
Posts: 1,546
Thanks: 77
Thanked 775 Times in 267 Posts
|
It's not in fact wrong at all. MySQL needs a boolean to evaluate the WHERE clause of the query, and coercing the integer return value of INSTR will have the desired result. INSTR returns 0 if the substring doesn't appear, which is coerced to boolean false. If the substring does appear, its index would be a positive integer, and any positive integer is coerced to boolean true.
|
| | |
| |
| The Following User Says Thank You to Dan Grossman For This Useful Post: |
|
| Bookmarks |
| Tags |
| mysql, query, simple |
| |