首先我不知道标题是否相关(如果不相关请原谅)。
我有 2 个表userprofile
和country
。
userprofile table:-
-----------------------------------------
userid | email |............|**countryid**
-----------------------------------------
country table:-
-----------------------------------------
**countryid** | countryname
-----------------------------------------
我想从userpfofile表中选择数据,
即 $query="select * from userprofile
...但问题是该数据返回countryid,但我实际上想返回countryname。
我是 MySQL 新手,所以请建议实现此目的的最佳方法。
最佳答案
您需要join将这两个表放在一起即可获取您想要的数据,如下所示:
SELECT * FROM userprofile u
LEFT JOIN country c
ON c.countryid = u.countryid
这将产生类似于以下内容的结果集:
---------------------------------------------------------
| userid | email | .......... | countryid | countryname |
---------------------------------------------------------
关于php - 如何使用ID从关联表中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11264421/