在设计数据库查询方面我非常糟糕,我可以帮忙吗?
以下是2个表格(样本):
TABLE user: id, name, relationship_type_id
TABLE relationship_type: relationship_type_id (fk), relationship, contributing
现在,我想使用单个查询来获取:
relationship_type
表的记录(即SELECT * FROM relationship_type
)relationship
id = 5
醇>
所以结果是这样的(假设表relationship_type
只有2条记录)
"data": [
{
"relationship": "Friend" // for user id = 5
},
{
"relationship_type_id": "1"
"relationship": "Partner"
},
{
"relationship_type_id": "2"
"relationship": "Friend"
}
],
答案 0 :(得分:1)
$filterArgs
答案 1 :(得分:0)
SELECT
rt.*, u.*
FROM
relationship_type rt
LEFT JOIN
user u
ON rt.relationship_type_id = u.relationship_type_id
WHERE
u.id = 5
UNION
SELECT *
FROM relationship_type;