#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0

    Exclamation Using plus sign within a column name?


    Hello I'm trying to work with a database that has a table with a column name like so:
    A_B+

    The problem is that every time I try to perform a query with it in any php document it returns a mysql error. I understand the + sign is a reserved word/symbol in the grammar but somehow there's an auto generated form using the same column name and it is able to work with it normally. I do not have access to that code so I can't see how do they get away with it.

    If you have any clue on how should I write this column name so that it's recognized by the engine without throwing an error please let me know!

    thanks in advanced.
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,031
    Rep Power
    4210
    Code:
    SELECT `A_B+`, ... -- use `backticks`
    r937.com | rudy.ca
    please visit Simply SQL and buy my book
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by r937
    Code:
    SELECT `A_B+`, ... -- use `backticks`
    That didn't work ... Still receiving the same error, here's the exact query:

    SELECT count(DISTINCT ID) AS Totals
    FROM table1 a
    WHERE a.x= 'Atlantic City' AND a.A_B+ = 'Yes' AND a.Date BETWEEN '2012-10-01' and '2012-11-08'

    I tried:
    SELECT count(DISTINCT ID) AS Totals
    FROM table1 a
    WHERE a.x= 'Atlantic City' AND `a.A_B+` = 'Yes' AND a.Date BETWEEN '2012-10-01' and '2012-11-08'

    Which gives me an SQL error ( SQL error (1054): Unknown column `a.A_B+` in 'where clause')

    I also tried:
    SELECT count(DISTINCT ID) AS Totals
    FROM table1 a
    WHERE a.x= 'Atlantic City' AND a.`A_B+` = 'Yes' AND a.Date BETWEEN '2012-10-01' and '2012-11-08'

    which returns 0 as result and 0 is not the correct count.
  6. #4
  7. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,031
    Rep Power
    4210
    Originally Posted by robertoronderos
    ...which returns 0 as result and 0 is not the correct count.
    well, at least you solved your error, right? the backticks worked

    as for why the query returned 0 rows, that's gotta be a data problem

    dump a few rows for us and we'll prove it actually did work
    r937.com | rudy.ca
    please visit Simply SQL and buy my book
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Originally Posted by r937
    well, at least you solved your error, right? the backticks worked

    as for why the query returned 0 rows, that's gotta be a data problem

    dump a few rows for us and we'll prove it actually did work
    No I'm using the same query for other columns and it works just fine, and also if I change the column name to another thing ( on my local machine , not on the live server ) it works fine as well.
  10. #6
  11. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,031
    Rep Power
    4210
    so the column name affects the count? i don't think so, bro

    like i said, dump a few rows for us
    r937.com | rudy.ca
    please visit Simply SQL and buy my book

IMN logo majestic logo threadwatch logo seochat tools logo