-4

I'm getting a strange error on mysql.

My query is:

SELECT 
    'u.id', 
    'u.primeiroNome', 
    'u.ultimoNome', 
    'u.email', 
    'p.cpf' 
FROM user as u 
INNER JOIN pacientes as p ON u.id = p.user_id

and here is the return https://i.stack.imgur.com/NSqyp.png

this is running on wamp 2.5 64bits

Donal
  • 31,121
  • 10
  • 63
  • 72
pcezar91
  • 111
  • 11
  • Why are you quoting the field name in the select statement? get rid of the quotes, try again – Twelfth Sep 04 '14 at 21:11
  • 1
    I am not a mysql expert...as a matter of fact I have never done a mysql query in my life,but why is the columns it quotes? It will put out x amount of rows with 'u.id', 'u.primeiroNome', 'u.ultimoNome', 'u.email', 'p.cpf' as the row values – Murdock Sep 04 '14 at 21:11
  • Therefore the columns will have "anonymous" names – Murdock Sep 04 '14 at 21:11
  • The result returned by this query is not at all strange, it's defined behavior. And it's not really a MySQL error at all. It's entirely valid to return a string literal as an expression in a SELECT list. If the intent is not to return literals, remove the single quotes, and MySQL will see those expressions in the SELECT list as identifiers of columns in the referenced tables. – spencer7593 Sep 04 '14 at 22:08

1 Answers1

4

Remove the simple quotes of your query:

SELECT u.id
    ,u.primeiroNome
    ,u.ultimoNome
    ,u.email
    ,p.cpf
FROM `user` as u
INNER JOIN pacientes as p ON p.user_id = u.id

Simple quotes are interpreted as string values and that the reason of your "anomaly" or problem.

Here is the same query but with use of backticks:

SELECT u.`id`
    ,u.`primeiroNome`
    ,u.`ultimoNome`
    ,u.`email`
    ,p.`cpf`
FROM `user` as u
INNER JOIN pacientes as p ON p.`user_id` = u.`id`

It's important to understand the distinction between single quotes and backticks when working with SQL queries.

Here you'll find more information about the use of these characters: When to use single quotes, double quotes, and backticks?

Hope this will help you.

Community
  • 1
  • 1
Joël Salamin
  • 3,538
  • 3
  • 22
  • 33