Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJLN CRML
30,362 PointsCommon column names after table JOIN - how do I select the one I want to use?
Hi, I have two tables, which I combined with an SQL JOIN statement. Both tables have the column "name". Now I want to display both of them, but it always only displays the second one.
I want it to work like this:
echo $row['name'] - of the first table
echo $row['name'] - of the second table
How do I code that?
Thanks a lot and have a great day! ^_^
1 Answer
thomascawthorn
22,986 PointsYou can do something like:
SELECT table1.name AS table1_name, table2.name AS table2_name FROM table1 INNER JOIN....
For future googling, this is called 'creating a column alias'.
In you're data array, you can then access like:
<?php
$data['table1_name'] // Name column from table 1
$data['table2_name'] // Name column from table 2
The alias can be in any format (it doesn't have to match the above example exactly).
SELECT cats.species AS catName, dogs.breed AS dogName FROM cats INNER JOIN....