[英]Alter columns from selected result table in MYSQL
Here the things,I want to join table ' responsibilities
' with fields Name, Direct, Supervise
: 在这里,我想加入表'
responsibilities
'与字段Name, Direct, Supervise
:
Name | Direct | Supervise
ABC 2 4
and table 'positions' with positionCode, positionID: 和positionCode,positionID的表'position':
positionCode | positionID
HR/HRM 2
HR/MN 4
The selected result table will be some thing like this. 选定的结果表将是这样的事情。
Name | Direct | Supervise
ABC HR/HRM HR/MN
The ' Direct
' and ' Supervise
' column should be positionCode
from 'positions' table. 在“
Direct
”和“ Supervise
”栏应该是positionCode
从“位置”表。 Is there an all-in-one query to output this result? 是否有一体化查询输出此结果? Or I have to query 2 times ?
或者我要查询2次?
Try this Query, 试试这个查询,
SELECT r.Name,
p1.positionCode AS Direct,
p2.positionCode AS Supervise
FROM responsibilities r
LEFT JOIN positions p1
ON r.Direct = p1.positionID
LEFT JOIN positions p2
ON r.Supervise = p2.positionID
Output: SEE SQLFiddle DEMO 输出: SEE SQLFiddle DEMO
I think you can join responsibilities
twice to the positions
table: 我想你可以加入
responsibilities
两次到positions
表:
SELECT r.Name,
COALESCE(p1.positionCode, 'Direct is N/A') AS Direct,
COALESCE(p2.positionCode, 'Supervise is N/A') AS Supervise
FROM responsibilities r
LEFT JOIN positions p1
ON r.Direct = p1.positionID
LEFT JOIN positions p2
ON r.Supervise = p2.positionID
Follow the link below for a running demo: 请点击以下链接查看正在运行的演示:
Try following Query, It should work 尝试以下查询,它应该工作
select R.Name,(select P.positionCode where R.Direct=P.positionID) as
Direct,(select P.positionCode where R.Supervise=P.positionID) as
Supervise from Responsibilites R, Positions P;
check out this SQL Fiddle for proof that this query works: 查看这个SQL Fiddle以证明此查询有效:
http://sqlfiddle.com/#!9/13845c/4/0 http://sqlfiddle.com/#!9/13845c/4/0
Basically, the query looks as follows: 基本上,查询如下所示:
SELECT r.Name, p1.positionCode As Direct, p2.positionCode as Supervise
FROM responsibilities r
LEFT JOIN positions p1 ON r.Direct = p1.positionID
LEFT JOIN positions p2 ON r.Supervise = p2.positionID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.