我对SQL很陌生,并尝试扩展到使用工会,所以我不能100%确定是否可以通过工会获得我想要的结果。
我的代码如下:
select workorder.wonum, null as description, null as quantity, null as unitcost, null as matlincost2, null as matlinprice,
labtrans.refwo, labtrans.startdate, labtrans.starttime, labtrans.finishtime, labtrans.regularhrs, labtrans.plusplineprice, labtrans.laborcode, labtrans.linecost as labline
from workorder
left join labtrans on labtrans.refwo=workorder.wonum and labtrans.siteid=workorder.siteid
where workorder.wonum='10192'
union
select matusetrans.refwo, matusetrans.description, matusetrans.quantity, matusetrans.unitcost, matusetrans.linecost as matlincost2, matusetrans.plusplineprice as matlinprice,
null as refwo, null as startdate, null as starttime, null as finishtime, null as regularhrs, null as plusplistprice, null as laborcode, null as labline
from matusetrans
where matusetrans.refwo='10192'
;
正如您所看到的,它将结果放在4个单独的记录上,但如果这有意义的话,我只希望结果只有2条记录。
可以使用union完成吗? 我尝试过加入而不是联盟,但无论如何它都引入了4条记录。
答案 0 :(得分:1)
你的工会产生了结果。 你必须看到你的空名:
select workorder.wonum, **null as description**, null as quantity, null as unitcost, null as matlincost2, null as matlinprice,
labtrans.refwo, labtrans.startdate, labtrans.starttime, labtrans.finishtime, labtrans.regularhrs, labtrans.plusplineprice, labtrans.laborcode, labtrans.linecost as labline
from workorder
left join labtrans on labtrans.refwo=workorder.wonum and labtrans.siteid=workorder.siteid
where workorder.wonum='10192'
union的第一部分,在描述中返回null(3行和4行)。
select matusetrans.refwo, matusetrans.description, matusetrans.quantity, matusetrans.unitcost, matusetrans.linecost as matlincost2, matusetrans.plusplineprice as matlinprice,
**null as refwo**, null as startdate, null as starttime, null as finishtime, null as regularhrs, null as plusplistprice, null as laborcode, null as labline
from matusetrans
where matusetrans.refwo='10192'
在第1行和第2行的refwo中返回null。
Union工作选择select中的所有结果并添加其他select:not group data,only add。
如果你有,例如,a 选择5行 联盟 选择3行
返回8行的select。
也许你可以使用联接,而不是@jens所说的联合,或联盟之后的联合,或选择的字段,你必须分析你需要的东西。