School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,354 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,802 people online right now. Registration is fast and FREE... Join Now!




SQL name conflicts

 

SQL name conflicts, in a join query

angrydwarfz

29 Jun, 2009 - 07:06 AM
Post #1

New D.I.C Head
*

Joined: 7 Dec, 2008
Posts: 20


My Contributions
Hello again, DIC community!

I'm having a problem with name conflicts in JOIN queries. I am using MySQL 5.0.67 running on an Apache 1 server. The query looks something like this:

CODE
SELECT star_systems.x, star_systems.y, star_systems.name AS planetname, users.name AS username FROM star_systems JOIN users ON star_systems.oid=users.id


However, even though they're from distinct tables, there seems to be a name conflict between star_systems.name and users.name, triggering the following SQL error...

QUOTE
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.name AS username FROM star_systems JOIN users ON star_systems.oid=use' at line 1


Thanks in advance,
angrydwarfz smile.gif

This post has been edited by angrydwarfz: 29 Jun, 2009 - 07:06 AM

User is offlineProfile CardPM
+Quote Post


Robin Marsh

RE: SQL Name Conflicts

29 Jun, 2009 - 07:41 AM
Post #2

New D.I.C Head
*

Joined: 7 May, 2009
Posts: 13



Thanked: 1 times
My Contributions
'Name' is a reserved word, try encasing it in square brackets...

CODE

SELECT
     star_systems.x,
     star_systems.y,
     star_systems.[name] AS [planetname],
     users.[name] AS [username]
FROM star_systems
JOIN users ON star_systems.oid = users.id



User is offlineProfile CardPM
+Quote Post

angrydwarfz

RE: SQL Name Conflicts

29 Jun, 2009 - 08:02 AM
Post #3

New D.I.C Head
*

Joined: 7 Dec, 2008
Posts: 20


My Contributions
Thanks for the reply, but, sadly, that doesn't seem to work. I get the exact same error when using the brackets.

If the error is because of the reserved word 'name' shouldn't simple queries like
CODE
SELECT name, email FROM users
be affected? Because so far, I've had no problems with them. This error only appears when using joins.
User is offlineProfile CardPM
+Quote Post

kmangold

RE: SQL Name Conflicts

29 Jun, 2009 - 08:13 AM
Post #4

D.I.C Head
Group Icon

Joined: 24 Jun, 2009
Posts: 166



Thanked: 7 times
Dream Kudos: 75
My Contributions
I just created some dummy tables and ran your original query. I get no errors? I have MySQL 5.1 installed on this machine. Are you sure you are using the right column names in the query?

I use reserved words all the time in my columns and queries (name, type, ...). In fact I just created a system where all the tables have a "name" AND "type" column.
User is offlineProfile CardPM
+Quote Post

angrydwarfz

RE: SQL Name Conflicts

29 Jun, 2009 - 08:34 AM
Post #5

New D.I.C Head
*

Joined: 7 Dec, 2008
Posts: 20


My Contributions
Thank you all for your help. Apparently I was simply very stupid and thought it was a name conflict problem when, in fact, the problem was a missing comma. I'm sorry for wasting your time. sad.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 06:58PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month