2017-07-20 51 views
1

我有5個表格或更多的日誌。在一個查詢中獲取5個表格數據...不加入...無關係

  1. tbl_product_log
  2. tbl_user_log
  3. tbl_customer_log
  4. tbl_vendor_log
  5. tbl_quotations_logs

他們相互沒有關係。但他們有相同的列的層次結構。

  1. LOGID
  2. logdetails
  3. logdatetime

時,也有一些是在這些表中發生(例如, '插入', '刪除',更新)的記錄每次插入相對錶中作爲日誌。

現在我想在一個頁面中顯示所有日誌。它有6個選項卡。

  1. 所有日誌
  2. 產品日誌
  3. 用戶登錄
  4. 客戶登錄
  5. 廠商會記錄
  6. 報價日誌

像這樣

enter image description here

在每個標籤例如(products logs, user logs, customer logs ....)我成功地獲取了所有日誌數據。

enter image description here

enter image description here

現在我想以顯示All標籤所有日誌數據,如在其他選項卡。

我試過了我自己的查詢,但它分別顯示了我的每件事情。

這裏是我的查詢

SELECT * FROM tbl_product_log, tbl_user_log, tbl_customer_log, tbl_vendor_log, tbl_quotations_logs Limit 20 

,這裏是導致

enter image description here

所以,請告訴我,我怎麼能做到這一點,所有的日誌數據All tab只有三列樣展示其他選項卡用一個查詢顯示結果。

你明白我的問題。

+0

既然你已經提取每個單一的表這將是可能更好地用PHP合併。這爲您節省了額外的選擇聯合聲明。 – user1915746

回答

2

使用工會的所有表連接

(Select * From tbl_product_log LIMIT 5) 

union 

(Select * From tbl_user_log LIMIT 5) 

union 

(Select * From tbl_customer_log LIMIT 5) 

union 

(Select * From tbl_vendor_log LIMIT 5) 

union 

(Select * From tbl_quotation_log LIMIT 5); 

說明:此查詢將產生總共25條記錄

+0

這是完美的工作...只是一件事......請告訴我如何申請'限額'條款,因爲它給了我1500+的數據 – deemi

+0

我申請限制......但它給了我錯誤 – deemi

+0

如何記錄你想在一個'全部'頁面? – derrysan7

0

嘗試使用UNION而不是

SELECT * FROM 
(SELECT logid, logdetails,logdatetime, '1' as identifier FROM tbl_product_log) as a 
UNION ALL 
(SELECT logid, logdetails,logdatetime, '2' as identifier FROM tbl_user_log) as b 
UNION ALL 
(SELECT logid, logdetails,logdatetime, '3' as identifier FROM tbl_customer_log) as c 
UNION ALL 
(SELECT logid, logdetails,logdatetime, '4' as identifier FROM tbl_vendor_log) as d 
UNION ALL 
(SELECT logid, logdetails,logdatetime, '5' as identifier FROM tbl_quotations_logs) as e 
-- WHERE indentifier = '' //in here you can put here to filter what report you wanted to appear 
ORDER BY logid // Order it by Logid 
LIMIT 20 //change the limit 

把一個標識符對每張表你與其他人結合,所以你仍然可以控制數據來自哪裏。

+0

兄弟你的Qury給我很多錯誤 '錯誤 靜態分析: 在分析過程中發現8個錯誤。 無法識別的關鍵字。 (靠近「as」位置187) 意外的令牌。 (位置190附近的「b」) 無法識別的關鍵字。 (在284位置附近「as」) 意外的標記。 (位置287附近的「c」) 無法識別的關鍵字。 (在379位置附近「as」) 意外的令牌。 (在位置382處的「d」附近) 無法識別的關鍵字。 (在「位置479附近」) 意外標記。(在位置482的「e」附近)' – deemi

+0

'SQL查詢:文檔 作爲UNION ALL(SELECT logid,logdetails,logdatetime,'SELECT * FROM(選擇logid,logdetails,logdatetime,'1'作爲標識符FROM tbl_product_log) 2'作爲標識符FROM tbl_user_log)作爲UNION ALL(選擇logid,logdetails,logdatetime,'4'作爲標識符FROM tbl_vendor_log)作爲UNION ALL(選擇logid,logdetails,logdatetime,'3'作爲標識符FROM tbl_customer_log) ALL(選擇logid,logdetails,logdatetime,'5'作爲標識符FROM tbl_quotations_logs)作爲e ORDER BY logid LIMIT 20' – deemi

+0

'MySQL說:文檔 #1064 - 您的SQL語法錯誤; (SELECT logid,logdetails,logdatetime,'3'作爲標識符FROM t'在第4行' – deemi

1

試試這個

SELECT * FROM (SELECT logid, logdetails,logdatetime, '1' as identifier 
FROM tbl_product_log 
UNION ALL 
SELECT logid, logdetails,logdatetime, '2' as identifier FROM tbl_user_log 
UNION ALL 
SELECT logid, logdetails,logdatetime, '3' as identifier FROM tbl_customer_log 
UNION ALL 
SELECT logid, logdetails,logdatetime, '4' as identifier FROM tbl_vendor_log 
UNION ALL 
SELECT logid, logdetails,logdatetime, '5' as identifier FROM tbl_quotations_logs) as a 
-- WHERE indentifier = '' -- in here you can put here to filter what report you wanted to appear 
ORDER BY logid -- Order it by Logid 
LIMIT 20 -- change the limit 
+0

已經測試過它和它的工作 –

+0

Thanx bro ...現在它的工作 – deemi

相關問題