Welcome Guest, Not a member yet? Register   Sign In
Column naming in joined tables
#1

[eluser]Ingonyama[/eluser]
If I have 2 tables events and categories and they both have columns with the same names:

events table columns:
id, title, description, category_id
categories table columns:
id, title, description, order_num

SELECT * FROM events JOIN categories ON events.category_id = categories.id

Will produce:
id, title, description, category_id, id, title, description, order_num

Is it standard practice to prefix with the table name when using this kind of join so we can tell which column we are dealing with? Or is there something I'm missing here?
#2

[eluser]Buso[/eluser]
[quote author="Ingonyama" date="1258093801"]If I have 2 tables events and categories and they both have columns with the same names:

events table columns:
id, title, description, category_id
categories table columns:
id, title, description, order_num

SELECT * FROM events JOIN categories ON events.category_id = categories.id

Will produce:
id, title, description, category_id, id, title, description, order_num

Is it standard practice to prefix with the table name when using this kind of join so we can tell which column we are dealing with? Or is there something I'm missing here?[/quote]

SELECT * FROM events AS a
JOIN categories AS b ON
a.category_id = b.id

For tables with more fields its nice to have these alias
#3

[eluser]Ingonyama[/eluser]
Thanks for the response but no luck, I'll try and clarify.. I want to end up with a joined result where the column names look something like this:

events.id, events.title, events.description, events.category_id, category.id, category.title, category.description, category.order_num

The code above seems to generate the same result as before, but maybe I'm doing something wrong?
#4

[eluser]clip[/eluser]
I think you will have to do something like:

SELECT events.id AS event_id, events.title AS event_title, category.title AS category_title... and so on




Theme © iAndrew 2016 - Forum software by © MyBB