1

我在 SQLite 中收到错误“FROM 附近的错误” ,但是我在此查询中找不到任何错误。我究竟做错了什么?

Select
tbltrans2_temp.itemcode,
tbltrans2_temp.itemname,
  Sum(tbltrans2_temp.qty) qty
From 
  tbltrans_temp Inner Join
  tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
 where
  tbltrans_temp.saleDate='11/07/2013'
Group By
  tbltrans2_temp.itemcode
  UNION
  Select
  tbltrans2.itemcode,
  tbltrans2.itemname,
  Sum(tbltrans2.qty) qty,
From 
  tbltrans Inner Join
  tbltrans2 On tbltrans2.transid = tbltrans.transid
   where
  tbltrans.saleDate='11/07/2013'
Group By
  tbltrans2.itemcode
4

1 回答 1

5

,在第二个FROM删除它之前有一个额外的:

Select
tbltrans2_temp.itemcode,
tbltrans2_temp.itemname,
  Sum(tbltrans2_temp.qty) qty
From 
  tbltrans_temp Inner Join
  tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
 where
  tbltrans_temp.saleDate='11/07/2013'
Group By
  tbltrans2_temp.itemcode
  UNION
  Select
  tbltrans2.itemcode,
  tbltrans2.itemname,
  Sum(tbltrans2.qty) qty,    <<<<-------------------  This
From 
  tbltrans Inner Join
  tbltrans2 On tbltrans2.transid = tbltrans.transid
   where
  tbltrans.saleDate='11/07/2013'
Group By
  tbltrans2.itemcode

更新

要从两个表中获取总和qty,将该查询放在子查询中并将其求和到外部查询中,您也可以sum在内部查询中省略 并且只在外部查询中执行:

SELECT 
  itemcode,
  itemname,
  SUM(qty) TotalQty
FROM
(

    Select
    tbltrans2_temp.itemcode,
    tbltrans2_temp.itemname,
      Sum(tbltrans2_temp.qty) qty
    From 
      tbltrans_temp Inner Join
      tbltrans2_temp On tbltrans2_temp.transID = tbltrans_temp.transid
     where
      tbltrans_temp.saleDate='11/07/2013'
    Group By
      tbltrans2_temp.itemcode
      UNION
      Select
      tbltrans2.itemcode,
      tbltrans2.itemname,
      Sum(tbltrans2.qty) qty
    From 
      tbltrans Inner Join
      tbltrans2 On tbltrans2.transid = tbltrans.transid
       where
      tbltrans.saleDate='11/07/2013'
    Group By
      tbltrans2.itemcode
) t
GROUP BY itemcode, itemname;
于 2013-07-11T12:07:07.570 回答