PHP MySQL 合并表问题,读取 fetch
我做了一个查询,连接同一个表 - users - 两次, 与加入。
SELECT * from tbl1
JOIN tbl2 ON tbl2.USER_ID = tbl1.ID
JOIN tbl2 as alias ON tbl1.ID = tbl2.TEACHER_ID
WHERE tbl1.ID = 45
对该表的第一次调用是常规的,第二次我使用 AS 的别名。 当我在 phpMyAdmin 中获得结果时,我看到结果不是被称为别名,而是使用通常的名称,如下所示:
USERNAME => Ted,ID_NUMBER=>33322552,...,USERNAME=>Josh ....
我如何读取该关联数组
I made a query that is joining the same table - users- twice,
with JOINs.
SELECT * from tbl1
JOIN tbl2 ON tbl2.USER_ID = tbl1.ID
JOIN tbl2 as alias ON tbl1.ID = tbl2.TEACHER_ID
WHERE tbl1.ID = 45
The first call to that table is regular, and the second time I use an alias using AS.
When I get the result in phpMyAdmin, I see the results are not referred to as the alias, but with the usual name, that's like this:
USERNAME => Ted, ID_NUMBER=>33322552, ... ,USERNAME=>Josh ....
How can I read that associative array
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此查询中,您没有使用别名,而是两次引用名为 tbl2 的真实表。这是行不通的。
将其重写为:
as
关键字是可选的。这样就不会误解您指的是哪个版本的 tbl2。
两个查询之间的差异
请注意,您还可以将查询 2 重写为:
在这种情况下,您可以将查询 1 重写为:
看到区别了吗?
In this query, you are not using an alias, you are refering to a real table called tbl2, twice. This will not work.
Rewrite it to:
The
as
keyword is optional.This way there can be no misunderstanding which version of tbl2 you are refering to.
The difference between the two queries
Note that you can also rewrite query 2 as:
In that case you would rewrite query 1 as:
See the difference?