我有此查询,该查询可返回在某个员工轮班时产生的发票
SELECT
i.dateTime,i.amount,i.totalProfit,i.shiftID,
i_o.itemID,i_o.quantity,
item.name itemName,
p.full_name
from invoice i
LEFT JOIN
inv_order i_o on i_o.invID=i.invID
LEFT JOIN
`item-service` item on item.itemID = i_o.itemID
LEFT JOIN person p on
p.PID=i.personID
where i.shiftID =97
但是然后,我需要从employee表中获取雇员名称,而我只有shiftID。
SELECT
i.dateTime,i.type,i.amount,i.totalProfit,i.shiftID,i_o.itemID,i_o.quantity,item.name itemName,p.full_name
from invoice i
LEFT JOIN
inv_order i_o on i_o.invID=i.invID
LEFT JOIN
`item-service` item on item.itemID = i_o.itemID
LEFT JOIN person p on
p.PID=i.personID
where i.shiftID =97
UNION
SELECT e.name from employee e
left join shift s on s.empID = e.empID
where s.shiftID =97
mysql返回此错误
使用的SELECT语句的列数不同
答案 0 :(得分:1)
关于该问题的错误消息非常清楚:您的第一个查询返回8列数据,而第二个查询仅返回1列,因此您不能UNION
。看来您可能只想通过JOIN
表employee
表shift
,例如
SELECT i.dateTime
,i.type
,i.amount
,i.totalProfit
,i.shiftID
,i_o.itemID
,i_o.quantity
,item.name AS itemName
,p.full_name
,e.name
FROM invoice i
LEFT JOIN inv_order i_o ON i_o.invID=i.invID
LEFT JOIN `item-service` item ON item.itemID = i_o.itemID
LEFT JOIN person p ON p.PID=i.personID
LEFT JOIN shift s ON s.shiftID = i.shiftID
LEFT JOIN employee e ON e.empID = s.empID
WHERE i.shiftID = 97