避免在SQL中重复列值

时间:2018-05-31 15:36:16

标签: mysql

This is my result screenshot

我需要避免重复字段Total,但我需要所有其他字段中的所有数据,任何人都可以帮助获得结果!?

这是我的SQL查询

SELECT 
  (SELECT SUM(credit.amount) 
  FROM credit,customers 
  WHEREcredit.date BETWEEN '2018-05-01' AND '2018-05-31' 
  AND credit.cus_id=customers.id) AS Total,
  credit.amount, credit.date,   
  customers.name 
FROM customers, credit 
WHERE credit.date BETWEEN' 2018-05-01' AND '2018-05-31' 
AND credit.cus_id=customers.id 
ORDER BY credit.date

1 个答案:

答案 0 :(得分:0)

您可以添加行计数器并将其与条件函数IF一起使用。如果行号是< = 1则运行子查询,否则它会打印一个空的varchar''。

正如我在评论中所说,我将考虑提取子查询并在单独的查询中运行它,然后将其结果添加到应用程序的第一行。

SET @rank=0;
SELECT @rank:=@rank+1 AS rank,
  IF(@rank>1,'',(SELECT SUM(credit.amount) 
  FROM credit,customers 
  WHEREcredit.date BETWEEN '2018-05-01' AND '2018-05-31' 
  AND credit.cus_id=customers.id)) AS Total,
  credit.amount, credit.date,   
  customers.name 
FROM customers, credit 
WHERE credit.date BETWEEN' 2018-05-01' AND '2018-05-31' 
AND credit.cus_id=customers.id 
ORDER BY credit.date