簡體   English   中英

從一個表MySQL中選擇一個差異報告

[英]Selecting a Diff report from one table MySQL

我一直試圖編寫一個MySQL查詢來處理單個表中Approved ='yes'Approved ='no'行的差異報告。 -我嘗試了幾種方法(包括自聯接和創建/查詢臨時表),無論我嘗試什么,最終都會得到錯誤的數據集輸出。

    SELECT DISTINCT a.*
    FROM myTable a
    JOIN myTable b
    ON a.apples = b.apples
    AND a.bananas = b.bananas
    AND a.oranges = b.oranges
    WHERE (
        a.Approved = 'no'
        AND b.Approved = 'yes'
    ) AND ( 
        a.diffVal1 <> b.diffVal1
        OR a.diffVal2 <> b.diffVal2
        OR a.diffVal3 <> b.diffVal3
    )

在這種情況下,我試圖比較具有相同蘋果,香蕉和橙子的行...而且,如果比較行之間的任何diffVal不相同,我想將該行包含在集合中正在輸出。 我不確定為什么這行不通,但是每次運行此命令時,它都會包括所有“否”條目,無論是否存在相同的蘋果,香蕉和橙子的“是”條目, diffVals。 (幾乎就像完全忽略了diffVal一樣)

我確定這是我忽略的愚蠢內容,但是如果您有任何建議,我將不勝感激。

填充表


    id  apples  bananas  oranges  diffVal1  diffVal2  diffVal3  Approved
    1   red     yellow   orange   a         b           c       yes
    2   red     yellow   orange   a         b           c       yes
    3   red     green    orange   a         b           c       yes
    4   red     yellow   orange   a         b           c       no
    5   red     yellow   orange   a         H           c       no
    6   green   yellow   orange   a         H           c       no
    7   red     yellow   orange   a         b           d       yes
    8   red     yellow   orange   a         b           e       yes
    9   red     yellow   orange   a         b           c       yes
    10  red     yellow   orange   a         b           c       yes
    11  red     yellow   orange   a         b           c       yes
    12  red     yellow   orange   a         b           c       yes
    13  red     yellow   orange   a         b           c       no
    14  red     yellow   orange   a         b           c       no
    15  red     yellow   orange   a         b           c       no
    16  red     yellow   orange   d         d           d       no

運行上述查詢的實際結果


    5   red yellow  orange  a   H   c   no
    16  red yellow  orange  d   d   d   no
    4   red yellow  orange  a   b   c   no
    13  red yellow  orange  a   b   c   no
    14  red yellow  orange  a   b   c   no
    15  red yellow  orange  a   b   c   no

如您所見,輸出中包含的行中,紅色黃色橙色的diffVals具有abc,盡管事實是“ yes”條目中紅色黃色橙色的diffVals具有abc。

預期成績


    5   red yellow  orange  a   H   c   no
    16  red yellow  orange  d   d   d   no

-我很抱歉格式化。 我嘗試添加屏幕截圖,但是由於我是新手,所以堆棧溢出不會讓我添加圖像。

一種方法

SELECT *
  FROM table1 t
 WHERE approved = 'no'
   AND EXISTS
(
  SELECT *
    FROM table1
   WHERE approved = 'yes'
     AND apples   = t.apples
     AND bananas  = t.bananas
     AND oranges  = t.oranges
)
   AND NOT EXISTS
(
  SELECT *
    FROM table1
   WHERE approved = 'yes'
     AND apples   = t.apples
     AND bananas  = t.bananas
     AND oranges  = t.oranges
     AND diffval1 = t.diffval1 
     AND diffval2 = t.diffval2 
     AND diffval3 = t.diffval3
);

輸出:

| ID | APPLES | BANANAS | ORANGES | DIFFVAL1 | DIFFVAL2 | DIFFVAL3 | APPROVED |
|----|--------|---------|---------|----------|----------|----------|----------|
|  5 |    red |  yellow |  orange |        a |        H |        c |       no |
| 16 |    red |  yellow |  orange |        d |        d |        d |       no |

這是SQLFiddle演示

暫無
暫無

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

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