2017-04-19 91 views
0

我已經完成了下面的查詢。我在c#網格視圖中使用這個。這個查詢一切正常。但正如你所看到的,有一張名爲「ocum」的表正在爲特定條件拉取數據。從兩個相同的結構化表中獲取mysql查詢

table "ocum" = has records before today . 
table "otrans" = has only today's records . 
  1. 兩個表結構是相同的。
  2. 我得到的數據完全來自「ocum」表的數據。
  3. 現在我想獲得今日的數據,即從otrans表。

簡而言之,今天是19-04-2017。所以ocum表包含所有記錄到18-04-2017和otrans表只包含19-04-2017記錄。在一天的時間關閉OTTR記錄被附加/添加到ocum表。

如何從單個語句中的兩個表中查詢數據。 這是我的查詢。

SELECT 
`ocum`.`tdate`, 
`ocum`.`damt`, 
`ocum`.`camt`, 
`ocum`.`narr`, 
@Bal := @Bal + `ocum`.`camt` - `ocum`.`damt` AS `bal` 
FROM `ocum`, (SELECT @Bal := 0) AS variableInit where `ocum`.`glcode` = "A03208" and `ocum`.`acno` = 40 
ORDER BY `ocum`.`tdate` ASC 
+1

如果我理解正確,你可以試試'UNION',https://dev.mysql.com/doc/refman/5.7/en/union.html –

回答

0

這聽起來像是使用聯合查詢可解決的問題。如果您之前沒有使用過聯合查詢,他們加入兩個具有相似結果集的選擇查詢並將結果組合到一個集合中。
例如

Select table1.field1 from table 1 [where ...] 
union 
    Select table2.field1 from table 2 [where ...] 

有關詳細信息,請參閱MySql和Union的教程,例如, My Sql Tutorial

+0

沒問題沒解決我試過看這個(SELECT 'ocum'.'tdate', 'ocum'.'damt', 'ocum'.'camt', 'ocum'.'narr', @Bal:= @Bal +'ocum'。 'camt' - 'ocum'.'damt' AS'bal' FROM'ocum',(SELECT @Bal:= 0)AS variableInit) 工會 (SELECT 'otrans'.'tdate', ' otrans'.'damt', 'otrans'.'camt', 'o從'otrans',(SELECT @Bal:= 0)AS variableInit,'''al'', @Bal:= @Bal +'otrans'.'camt'''''''''''' ) 其中glcode =「L04502」和acno = 641 –

+0

惠惠!它解決了我的問題。 –

+0

很高興你解決了這個問題:你很明白,它自己的每個選擇查詢都需要完成。這是我第一次嘗試看到的唯一錯誤。 (第一次嘗試只包括第二個Select語句中的'where'子句)。如果我的答案解決了您的問題,那麼您可以將其標記爲接受的答案? – Charemer

相關問題