如何从两个表中检索值

时间:2015-04-24 12:08:56

标签: mysql

我有两个表,一个是stockstore和另一个费用,我想在两个日期之间计算损益搜索。所以我想在文本字段中给出值时计算总费用金额。

stockstore表

          id| date       | ItemName | Price
          1   09/04/2015   Apple       40
          2   10/04/2015   Banana      30
          3   12/04/2015   Salt        10 

费用表

         id | Date       |Expenses Type| Amount
          1   14/04/2015   Travel      |100
          2   17/04/2015   Decoration   200

我希望在09/04/201517/04/2015之间的两个日期之间按日期搜索时,它应该显示为

        id| date       | ItemName | Price
          1   09/04/2015   Apple       40
          2   10/04/2015   Banana      30
          3   12/04/2015   Salt        10 
          1   14/04/2015   Travel      100
          2   17/04/2015   Decoration  200

2 个答案:

答案 0 :(得分:1)

也许你可以这样做:

SELECT
    stockstore.id,
    stockstore.date,
    stockstore.ItemName,
    stockstore.Price
FROM
    stockstore
WHERE
    stockstore.date BETWEEN '09/04/2015' AND '17/04/2015'
UNION ALL
SELECT
    Expenses.id,
    Expenses.date,
    Expenses.`Expenses Type`,
    Expenses.Price
FROM
    Expenses
WHERE
    Expenses.date BETWEEN '09/04/2015' AND '17/04/2015'

答案 1 :(得分:-1)

这是一种简单的方法。

stockstore = SELECT Price FROM stockstore WHERE date BETWEEN #07/04/1996# AND #07/09/1996#;
Expenses = SELECT Amount FROM Expenses WHERE Date BETWEEN #07/04/1996# AND #07/09/1996#;

foreach(stockstores as stockstore)
    //add up stockstore
foreach(Expenses as Expense)
    //add up Expense

profit = stockstore - Expense