0

I found a table i am working in to have column names equal to mysql keywords. Great. That can be solved by " or ' like this example:

select count("count") from A;

will work when count is a column name as well as a keyword for Mysql.

but i have no idea how to do it when you need to leverage joins.

The following code doesn't seem to work, but you all will have an idea as to what i am trying to do.

select count(a."count") from A a join B b on a.id = b.id;

it seems that a."count" is not valid. How would i go about resolving this issue so i can adjust all of my queries accordingly.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • 1
    `COUNT("count")` actually does _not_ do what you intend, thought the syntax is valid. It is counting the literal string `"count"`. You must use backticks for column or table names in MySQL: `COUNT(\`count\`)` and `COUNT(a.\`count\`)` rather than single or double quotes. – Michael Berkowski Oct 08 '15 at 01:46
  • ohhhh, let me check to confirm – Fallenreaper Oct 08 '15 at 01:48
  • Confirmed. Thank you for the info on backticks! That is the answer i need! – Fallenreaper Oct 08 '15 at 01:53
  • The reason you get the correct result for `COUNT("count")` is because the _string_ `"count"` is a non-null value. As a string literal, it has the same value for every row in the result set and the aggregate `COUNT()` therefore counts it. If some of the actual values of your `count` column were null, you would get the wrong result because they'd be counted with `"count"` when otherwise they wouldn't. – Michael Berkowski Oct 08 '15 at 01:56
  • makes sense. I also noticed that that correct query i needed was sum(count), which was kicking back errors until the ticks were emplaced. Thank you @MichaelBerkowski – Fallenreaper Oct 08 '15 at 02:41

0 Answers0