PostgreSQL & PHP Tutorials - Database Joins - Table Aliases
Viewing page 4 of 4
« |
Back | Next | »
Another thing we can do to make this easier is to use a table alias. This is a "shortcut" to make our queries easier to read.
SELECT * FROM newsitem n INNTER JOIN authors a ON n.authorid=a.authorid;
We use "n" instead of "newsitem" and "a" instead of "authors". In this example we don't see much difference but if we selectively pick out our fields:
SELECT n.newsid, n.newstitle, n.newscontent, a.username, a.firstname, a.lastname FROM newsitem n INNER JOIN authors a ON n.authorid=a.authorid;
We can quickly see which field comes from which table and this saves us typing the whole table name.
In this particular case there's no real benefit from using table aliases, but try joining 3-4 tables together and you'll see a lot of use for it.
There you have it!
Viewing page 4 of 4
« |
Back | Next | »
Avg Rating: 3
Vote Count: 9
