0

我有一个income看起来像这样的表:

date              income      
---------------------------
09/05/13          56000    
09/05/13          66600
09/05/13          50000

还有一个expense看起来像这样的表:

date              expense 
----------------------------
09/05/13          68800

我想编写一个查询,其输出如下所示:

date              income             expense 
---------------------------------------------
09/05/13          56000              68800
09/05/13          66600
09/05/13          50000

每个值 fromincome.income和每个值 fromexpense.expense只出现一次。(如果我做一个简单的连接,那么每个连接都会出现三次,因为income.date并且expense.date有重复的值。)

4

5 回答 5

2

如果您在没有任何唯一 ID 的情况下尝试这样,那么您的完整概念是错误的。在您的表中添加一些唯一的 id 并进行必要的编码。

如果你有像下面这样的表结构,你可以用简单的 equi join 编写查询。收入表:

date              income  id    
---------------------------
09/05/13          56000   1 
09/05/13          66600   2
09/05/13          50000   3

费用表:

date              expense  id
----------------------------
09/05/13          68800    1
09/05/13                   2
09/05/13                   3

(或)尝试@Brian Hoover 的查询它会起作用。

 SELECT income.date_col, income.income, expense.expense
FROM (
        SELECT i.date_col, i.income, @curRow := @curRow + 1 AS row_number
        FROM Income_tbl i
        JOIN (SELECT @curRow := 0) r
     ) AS income
JOIN (
        SELECT e.date_col, e.expense, @curExpenseRow := @curExpenseRow + 1 AS row_number
        FROM Expense_tbl e
        JOIN (SELECT @curExpenseRow := 0) r
     ) AS expense
ON income.row_number = expense.row_number;      
于 2013-09-04T17:09:05.537 回答
1

最简单的方法是计算总和(按日期分组)并加入它们。您将需要三个查询来表示您拥有的三个数据集:日期集、收入集和费用集。

第一:设定的日期

select distinct `date`
from (select `date` from income union select `date` from expense)

二:收入设定:

select `date`, sum(i.income) as income
from income as i
group by `date`

三:费用设置:

select `date`, sum(e.expense) as expense
from expense as e
group by `date`

最后:把它们放在一起:

select 
    d.date, i.income, e.expense
from
    (
        select distinct `date` 
        from (select `date` from income union select `date` from expense)
    ) as d
    left join (
        select `date`, sum(i.income) as income
        from income as i
        group by `date`
    ) as i on d.`date` = i.`date`
    left join (
        select `date`, sum(e.expense) as expense
        from expense as e
        group by `date`
    ) as e on d.`date` = e.`date`
于 2013-09-04T16:37:26.917 回答
0

这会给你你要求的输出,但它真的很脆弱。

SELECT income.date, income.income, expense.expense
FROM (
SELECT i.date, i.income, @curRow := @curRow + 1 AS row_number
FROM income i
JOIN (SELECT @curRow := 0) r) AS income
JOIN (
SELECT e.date, e.expense, @curExpenseRow := @curExpenseRow + 1 AS row_number
FROM expense e
JOIN (SELECT @curExpenseRow := 0) r) AS expense
ON income.row_number = expense.row_number

SQL小提琴

假设您的收入和支出的行数相同,并且收入和支出的顺序相同,因此它是按行号合并结果。

你真正需要的是某种方式来加入收入和支出,可能是分类帐条目 ID 或每一行的东西。与我发布的查询中的被黑连接相比,这将为您提供确定的连接。

于 2013-09-04T16:51:49.517 回答
0

如果你想要单独的条目,你需要这样的东西:

select date, income, expense
from
  (
    select
      if(@iId_last = incomeId, null, a.income) as income,
      if(@eId_last = expenseId, null, a.expense) as expense,
      a.date,
      @iId_last := a.incomeId as incomeId,
      @eId_last := a.expenseId as expenseId
    from 
      (select @d := '0000-00-00', @iId_last := -1, @eId_last := -1) as init,
      (
        select 
          d.*, 
          coalesce(i.incomeId,0) as incomeId, income, 
          coalesce(e.expenseId,0) as expenseId, expense
        from
          (
            select distinct date from (select incomeDate as date from income union select expenseDate as date from expense) as d
          ) as d
          left join income as i on d.date = i.incomeDate
          left join expense as e on d.date = e.expenseDate
        order by d.date, incomeId, expenseId
      ) as a
    order by date, incomeId, expenseId
  ) as r;

请注意,此解决方案要求incomeexpense表都有一个 Id。检查SQL Fiddle 解决方案(我已经测试了一些组合:许多收入行和一个支出行,一个收入行和许多支出行,没有收入行和一些支出行,一个收入行和没有支出行)。

于 2013-09-04T17:09:34.070 回答
0

如果您不关心收入值与费用值匹配的特定顺序,您可以使用这样的查询获得所需的输出

SELECT date, 
       MAX(CASE WHEN type = 1 THEN amount END) income,
       MAX(CASE WHEN type = 2 THEN amount END) expense
  FROM
(
  SELECT 1 type, date, income amount, @n := IF(@g = date, @n + 1, 1) rnum, @g := date g
    FROM income CROSS JOIN (SELECT @n := 0, @g := NULL) i1
   UNION ALL
  SELECT 2, date, expense amount, @m := IF(@f = date, @m + 1, 1) rnum, @f := date g
    FROM expense CROSS JOIN (SELECT @m := 0, @f := NULL) i2
) q
 GROUP BY date, rnum

输出:

| 日期 | 收入 | 费用 |
|--------------------|--------|---------|
| 2013 年 9 月 5 日 | 56000 | 68800 |
| 2013 年 9 月 5 日 | 66600 | (空) |
| 2013 年 9 月 5 日 | 50000 | (空) |

这是SQLFiddle演示

于 2013-09-05T05:31:27.813 回答