When I’m selecting from multiple tables that share column names is there a way I can return both, but define which one I want to select the data from?
For instance:
Both tables contain “date” columns, and I want to use both, but I would like to avoid having to rename each column that has duplicate names.
$query = mysql_query("SELECT * FROM posts, comments"); //(SELECT * is just for example)
while($row=mysql_fetch_array($query))
{
$postDate = $row['date']; //I would like to be able to do something like:
//$postDate = $row['posts']['date']; OR $row['posts.date'];
//of course it's all in an array now, jumbled up.
$commentDate = $row['date'];
}
You need to alias the duplicate column names in your query if you want both, eg
Then use the aliases to retrieve the values
FYI, it’s almost never a good idea to
SELECT *, especially when multiple tables are involved. You should always try to be specific about the columns added to yourSELECTclause