我被困在mysql查询上,我想要一个新列(总行)来显示返回行的总数,所以例如返回行是5,我希望每列(总行)在每个单独显示5行。但是,当我在同一查询中执行分组时,它会将其拆分并在每个中显示1。我如何做到这一点,以免它被"组打扰,可能是计数的总和?
我原来的查询我尝试失败了:
select curdate() as todays_date, qty as Quantity, plu as Item_ID, item as Description,
sum(count(distinct plu)) from transact where qdate between '2013-07-01' and '2014-07-10'
and location ='8' group by plu;
sum(count(distinct plu))不起作用,任何执行相同功能的查询?
由于
编辑:
以下是查询的一些结果。
'2014-08-08', '6', '1', '', 'MUM NAME RIBBON', '1'
'2014-08-08', '8', '1', '698-1284C-01-AS', 'Fearless Friar Sweatshirts AS', '1'
'2014-08-08', '8', '1', '698-1584-01-L', 'Shield Sweatshirts (new & old) L', '1'
'2014-08-08', '8', '1', '718103072885', 'Composition Book', '1'
'2014-08-08', '8', '1', '93546AS', 'Sweatshirt - Grey Friars - AS', '1'
'2014-08-08', '4', '1', 'AA01', 'Girls Soccer Game Ticket', '1'
'2014-08-08', '4', '1', 'AA04', 'BASEBALL HATS', '1'
'2014-08-08', '4', '1', 'AA06', 'BASKETBALL FEES', '1'
答案 0 :(得分:0)
你不能在一个查询中对一个总和进行计数..这是无效的语法..但是对于你想要做的事情,你需要一个单独的查询来完成计数并且不受原始查询的影响,如此:
SELECT
t.todays_date, t.Quantity, t.Item_ID, t.Description, t1.num_plu
FROM
( SELECT
CURDATE() AS todays_date,
qty AS Quantity,
plu AS Item_ID,
item AS Description
FROM transact
WHERE qdate BETWEEN ('2013-07-01' AND '2014-07-10')
AND location ='8'
GROUP BY counter
) t
JOIN
( SELECT
plu, COUNT(DISTINCT plu) AS num_rows
FROM transact
WHERE qdate BETWEEN ('2013-07-01' AND '2014-07-10')
AND location ='8'
) t1 on t1.plu = t.Item_ID