簡體   English   中英

從兩個相同的結構化表中獲取mysql查詢

[英]get mysql query from two same structured tables

我已經完成了以下查詢。 我在c#網格視圖中使用它。 這個查詢一切都很好。 但是,正如您所看到的,有一個名為“ ocum”的表正在針對特定條件提取數據。

table "ocum" = has records before today . 
table "otrans" = has only today's records . 
  1. 兩個表的結構相同。
  2. 我今天從“ ocum”表獲得的數據非常完美。
  3. 現在我想從otrans表中獲取Today的數據。

簡單來說,今天是19-04-2017。 因此ocum表包含截至18-04-2017的所有記錄,而otrans表僅包含19-04-2017記錄。 在一天的關閉時間,otrans記錄將追加/添加到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

這聽起來像是通過使用Union查詢可以解決的問題。 如果您以前沒有使用過聯合查詢; 它們將具有相似結果集的兩個選擇查詢聯接在一起,並將結果合並為一個集合。
例如

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

有關更多信息,請參見MySql和Union的教程,例如My Sql Tutorial。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM