我有以下表格
[users]
id BIGINT,
name VARCHAR(255),
email VARCHAR(255),
role ENUM('admin', 'executive', 'pos')
...
[transactions]
id BIGINT
fname VARCHAR(255),
lname VARCHAR(255),
mobile CHAR(10),
email VARCHAR(255),
amount FLOAT(7, 2),
verified_by BIGINT NOT NULL # Refers to user.id
...
[refunds]
id BIGINT
transaction_id BIGINT,
amount FLOAT(7,2)
initiated_by BIGINT NOT NULL, # Refers to user.id
processed_by BIGINT NULL # Refers to user.id
...
我想加入退款表,以便显示交易信息以及refunds.initiated_by和refunds.processed_by的users.name。由于退款是由具有角色pos的用户发起的,后来由具有角色执行的其他用户处理。我不想将用户表分成用户,主管和pos,除了角色,所有其他字段对于这三个都是相同的。
目前,我可以获取交易信息以及发起退款的用户的姓名。
SELECT refunds.id, fname, lname, mobile, transactions.email,
transactions.amount AS transaction_amount, refunds.amount AS refund_amount,
users.name AS initiated_by, refunds.processed_by
FROM refunds
JOIN transactions
ON refunds.transaction_id=transactions.id
JOIN users
ON users.id=refunds.initiated_by;
这给了我除了处理退款的用户名之外的所有内容。我怎么做到的?
提前致谢!
答案 0 :(得分:1)
我认为您可以再次加入users
表格,以引入处理退款的用户名称:
SELECT r.id,
t.fname,
t.lname,
t.mobile,
t.email,
t.amount AS transaction_amount,
r.amount AS refund_amount,
u1.name AS initiated_by,
u2.name AS processed_by,
r.processed_by
FROM refunds r
INNER JOIN transactions t
ON r.transaction_id = t.id
INNER JOIN users u1
ON u1.id = r.initiated_by
INNER JOIN users u2
ON u2.id = r.processed_by