1

What's wrong with this query?

SELECT
    `category`,
    (
        SELECT `name`
        FROM `city_names`
        WHERE `city_code`=`units`.`city_code`
    )as `cityName`,
    `email`
FROM
    `units`
WHERE
    (
        SELECT COUNT(*) 
        FROM `pesquisaRH` 
        WHERE `unit_code` = `units`.`unit_code`
    )=0 AND
    (
        (`category` LIKE `%central%`) OR 
        (`category` LIKE `%DPM%`) OR 
        (`category` LIKE `%DPC%`) OR  
        (`category` LIKE `%DIC%`)
    )

It returns me this error:

#1054 - Coluna '%central%' desconhecida em 'where clause'

It seems to be searching for the patterns in the column name instead of its value.

I want to find the units that didn't answer a questionary (the ondes that count 0 when searching the "pesquisaRH" table) AND have one of the four specifyed patterns in their category names (%central%, %DPM%, %DPC% or %DIC%); and then, return their category, the name of the city they're located and their email.

Henrique Müller
  • 157
  • 1
  • 2
  • 11
  • 1
    Only use backticks ` for reserved words. If you drop all backticks and use normal `'` quotes on the `LIKE '%sghds%'` clause your code will A: work and B: not give me a migraine. – Johan Sep 27 '11 at 13:43
  • see http://stackoverflow.com/questions/261455/using-backticks-around-field-names – zmf Sep 27 '11 at 13:45

1 Answers1

5
`%central%` should be '%central%'

`%DPM%` should be '%DPM%'

... and so on. You're using the wrong kind of quote!

awm
  • 6,526
  • 25
  • 24