Help with simple MySQL Query

by 5 comments
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:
#programming #mysql #query #simple
  • Profile picture of the author Dan Grossman
    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
    WHERE uid = 124 AND title LIKE '%21.01%'
  • Profile picture of the author KirkMcD
    For that query, no. In fact it is wrong.
    instr returns the position of the substring in the main string.

Next Topics on Trending Feed