mysql - 我可以跨 3 个共享多个主键的表进行 mysql 查询吗?

标签 mysql

我有这个查询来获取 user_id 的所有请求,并从与检索到的记录关联的 profile_id 中获取配置文件名称。

SELECT Requests.request_id,Requests.user_id,Requests.profile_id,Requests.job_title,Requests.date,Requests.time,Requests.info,Requests.approval, Profile.first_name, Profile.last_name FROM Requests, Profile WHERE user_id = '$user_id' AND Requests.profile_id = Profile.profile_id ORDER BY approval ASC

如果我想添加另一个表来从 Accounts 表中检索 user_id 的名称该怎么办?

最佳答案

我认为使用联接是一个好主意,因为它更清晰。

SELECT Requests.request_id,Requests.user_id,Requests.profile_id,Requests.job_title,Requests.date,Requests.time,Requests.info,Requests.approval, Profile.first_name, Profile.last_name, OtherTable.other_field FROM Requests 
LEFT JOIN Profile ON Requests.profile_id = Profile.profile_id 
LEFT JOIN OtherTable ON Requests.profile_id = OtherTable.profile_id 
WHERE Request.user_id = '$user_id' ORDER BY approval AS

关于mysql - 我可以跨 3 个共享多个主键的表进行 mysql 查询吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4617558/

相关文章:

java - 错误 org.hibernate.util.JDBCExceptionReporter - 通信链路故障

php - 页面刷新 WP PHP

mysql - libmemcached 路径?

php - 如何将此 xml feed 导入到 sql 中并自动更新?

php - sql中的引号导致问题

mysql - 如果行值对同一个人 ID 重复,我需要在不同的列中显示行值

php - 按邮政编码/经度/纬度选择结果

mysql - 将三个 MySQL 查询结果合并为一个排序正确的结果

php - 使用 PDO 的正确信息登录失败

MySQL UNIQUE约束多列条件