客户请求:每日报告两个DB表之间的变化

时间:2014-12-02 09:17:47

标签: html sql report

今天我的客户向我提出了一个请求:向他报告我们数据库的两个表格之间的变化以及每日报告。 实际上,我在想什么是最好(和更聪明)的方法。

  • SQL程序?
  • HTML页面?

我不知道该怎么做。如何标记两个表之间的更改? 我试着用一个例子来解释一下:

YESTERDAY TABLE:

+----+----------+----------+-----------+----------------+
| ID | HOSTNAME |    IP    |  STATUS   |      NOTE      |
+----+----------+----------+-----------+----------------+
| 01 | TEST_HST |127.0.0.1 |  ACTIVE   |Lorem Ipsumm    |
+----+----------+----------+-----------+----------------+
| 02 | TST2_HST |10.6.10.1 |   DOWN    |Lorem Ipsumm    |
+----+----------+----------+-----------+----------------+
| 03 | TST3_HST |10.6.10.2 |  ACTIVE   |Lorem Ipsumm    |
+----+----------+----------+-----------+----------------+

TODAY TABLE

+----+----------+----------+-----------+--------------+-----------------------------------+
| ID | HOSTNAME |    IP    |  STATUS   |     NOTE     |             TODAY_NOTES           |
+----+----------+----------+-----------+--------------+-----------------------------------+
| 01 | TEST_HST |127.0.0.1 |   DOWN    |Lorem Ipsumm  |STATUS: DOWN                       |
+----+----------+----------+-----------+--------------+-----------------------------------+
| 02 | TST2_HST |10.6.10.1 |  ACTIVE   |Bla bla bla   |STATUS: ACTIVE, NOTE: Bla bla bla  |
+----+----------+----------+-----------+--------------+-----------------------------------+
| 03 | TST3_HST |10.6.10.2 |   DOWN    |Bla bla BLA   |STATUS: DOWN, NOTE; Bla bla BLA    |
+----+----------+----------+-----------+--------------+-----------------------------------+

我想向我的客户报告(以表格形式)标记这两个表之间的差异。你认为最好的方法是什么?

感谢大家的支持。

1 个答案:

答案 0 :(得分:0)

这样的东西应该能够获得旧的和新的值,你如何显示这些数据取决于你。

SELECT yt.*, tt.Status AS NewStatus, tt.Note AS NewNote 
FROM YesterdayTable yt
INNER JOIN TodaysTable tt
ON yt.ID = tt.ID
WHERE vt.Status <> tt.Status OR vt.Note <> tt.Note

这会给你:

+----+----------+----------+-----------+----------------+
| ID | HOSTNAME |    IP    |  STATUS   |      NOTE      |NewStatus | NewNote
+----+----------+----------+-----------+----------------+
| 01 | TEST_HST |127.0.0.1 |  ACTIVE   |Lorem Ipsumm    | DOWN     | Lorem Ipsum
+----+----------+----------+-----------+----------------+
| 02 | TST2_HST |10.6.10.1 |   DOWN    |Lorem Ipsumm    | ACTIVE   | Bla bla bla
+----+----------+----------+-----------+----------------+
| 03 | TST3_HST |10.6.10.2 |  ACTIVE   |Lorem Ipsumm    | DOWN     | Bla bla bla
+----+----------+----------+-----------+----------------+