我有两个表,每个表都有一个“id”字段。这里有两个表:PHP + MySQL:当我从两个表中选择时,如何定义我想从哪个表中获取“id”
news : id, authorid, datetime, body
authors : id, name
我有这样的选择查询:
SELECT * FROM news, authors WHERE (news.authorid = authors.id) ORDER BY news.datetime DESC;
然后,在PHP中,我想使用的表的消息ID。我有这样的代码:
while ($row = mysql_fetch_array($result))
{
// I want to take news.id, but it gives an error:
echo $row["news.id"]; // error: "Undefined index: news.id"
// And when I write only "id", it takes it of authors.
echo $row["id"]; // this id is authors.id, but I want news.id
}
谢谢! +1因为星号 – 2011-04-13 12:00:27
尽管如此,你不应该真的使用星号,具体来说你正在检索哪些信息。在以后的日子里,表格模式可能会增长,然后你会选择很多额外的列没有很好的理由... – philm 2011-04-13 12:26:43
@philm你是对的。我的技术主管(他坐在我的对面)一直说这是一个咒语,这就是为什么我包括这两个例子:) – JohnP 2011-04-13 12:28:18