SELECT作为同一个表的子集

时间:2012-11-19 13:37:39

标签: mysql

我正在尝试做这样的事情,所以结果是两个单独选择的组合

SELECT
order_number,placed_by,placed_when,updated_by,updated_when,
(SELECT SUM(qty_order<>qty_rec) 
 FROM 1_purchase_orders 
 WHERE order_number=order_number AND line_number<>0)
WHERE
supp_number=4 AND line_number=0;

1 个答案:

答案 0 :(得分:1)

您在查询中使用的相关子查询必须只返回一个标量值。像这样:

SELECT 
  t1.order_number,
  t1.placed_by,
  t1.placed_when,
  t1.updated_by,
  t1.updated_when, 
  (SELECT SUM(t2.qty_order <> t2.qty_rec) 
   FROM 1_purchase_orders t2
   WHERE t2.order_number = t1.order_number AND t2.line_number <> 0 
   ORDER BY qty_order LIMIT 1) 
FROM 1_purchase_orders t1
WHERE t1.supp_number = 4 AND t1.line_number=0;