删除/隐藏具有' Pass'的类似记录结果

时间:2016-08-01 06:08:11

标签: php mysql

您好我已经坚持这个问题好几天了。我不确定哪些代码 我可以使用(选择if或者使用2个不同的查询?)

这是我当前代码显示表格的方式

+-----------------------------------------------------------------+
| id    |  class  |  direction  |  test  |  results | Primary Key |
+-----------------------------------------------------------------+
| 22    |    A    |   left      |  abc   |  Fail    |  45678963   |
| 22    |    A    |   left      |  abc   |  Pass    |  45998963   |
| 22    |    A    |   right     |  abc   |  Fail    |  98578963   |
| 22    |    A    |   right     |  abc   |  Pass    |  13578763   |    
| 22    |    A    |   left      |  xyz   |  Pass    |  35553624   |
| 22    |    A    |   right     |  xyz   |  Pass    |  54889541   |
| 22    |    B    |   left      |  abc   |  Pass    |  87895412   |
| 22    |    B    |   right     |  abc   |  Pass    |  88845644   |
| 22    |    B    |   left      |  xyz   |  Pass    |  96232142   |
| 22    |    B    |   right     |  xyz   |  Pass    |  11123447   |
| 33    |    A    |   left      |  abc   |  Pass    |  12134597   |
| 33    |    A    |   right     |  abc   |  Pass    |  23654668   |
| 33    |    A    |   left      |  xyz   |  Fail    |  78454675   |
| 33    |    A    |   left      |  xyz   |  Pass    |  98754651   |
| 33    |    A    |   right     |  xyz   |  Pass    |  69865232   |
| 33    |    B    |   left      |  abc   |  Fail    |  22235652   |
| 33    |    B    |   left      |  abc   |  Pass    |  85454555   |
| 33    |    B    |   right     |  abc   |  Pass    |  98665423   |
| 33    |    B    |   left      |  xyz   |  Pass    |  22115645   |
| 33    |    B    |   right     |  xyz   |  Pass    |  56654789   |
+-----------------------------------------------------------------+  

但这是我需要的表格

               +---------------------------------------------------+
               | id    |  class  |  direction  |  test  |  results |
               +---------------------------------------------------+
               | 22    |    A    |   left      |  abc   |  Fail    |
##Delete/Hide##| 22    |    A    |   left      |  abc   |  Pass    |##Delete/Hide##
               | 22    |    A    |   right     |  abc   |  Fail    |
##Delete/Hide##| 22    |    A    |   right     |  abc   |  Pass    |##Delete/Hide##    
               | 22    |    A    |   left      |  xyz   |  Pass    |
               | 22    |    A    |   right     |  xyz   |  Pass    |
               | 22    |    B    |   left      |  abc   |  Pass    |
               | 22    |    B    |   right     |  abc   |  Pass    |
               | 22    |    B    |   left      |  xyz   |  Pass    |
               | 22    |    B    |   right     |  xyz   |  Pass    |
               | 33    |    A    |   left      |  abc   |  Pass    |
               | 33    |    A    |   right     |  abc   |  Pass    | 
               | 33    |    A    |   left      |  xyz   |  Fail    |
##Delete/Hide##| 33    |    A    |   left      |  xyz   |  Pass    |##Delete/Hide##
               | 33    |    A    |   right     |  xyz   |  Pass    |
               | 33    |    B    |   left      |  abc   |  Fail    |
##Delete/Hide##| 33    |    B    |   left      |  abc   |  Pass    |##Delete/Hide##
               | 33    |    B    |   right     |  abc   |  Pass    |
               | 33    |    B    |   left      |  xyz   |  Pass    |
               | 33    |    B    |   right     |  xyz   |  Pass    |
               +---------------------------------------------------+

我想用" Pass"删除行。只要有类似的身份证, 类,方向,测试和结果都失败了。

我不确定如何正确编码。任何帮助,将不胜感激。 谢谢。

这是我用来从数据库中获取数据的代码

$query= "SELECT DISTINCT `id`, class, `direction`,`test`, `results` FROM `table`";
   $result=mysqli_query($connect, $query);

   while($row = mysqli_fetch_array($result)){
        echo $row[0];
        echo $row[1];
        echo $row[2];
        echo $row[3];
        echo $row[4];
   }

3 个答案:

答案 0 :(得分:2)

以下是DELETE查询:

DELETE YT 
FROM your_table YT
INNER JOIN
(
    SELECT 
    id,
    class,
    direction,
    test
    FROM your_table
    WHERE results ='Fail'
    GROUP BY id,class,direction,test
    HAVING COUNT(*) >= 1
) AS t 
ON YT.id = t.id AND YT.class = t.class AND YT.direction = t.direction AND YT.test = t.test 
WHERE YT.results = 'Pass'

<强>解释

内部查询将仅返回具有fail状态的行至少一次。

然后在主表和内部查询返回的结果之间生成inner join将生成包含具有failpass状态的记录的结果。

稍后仅根据Pass状态过滤这些记录将只保留您要删除的记录。

编辑:

为了隐藏此类记录而不是删除,您需要引入一个名为is_deleted的列,该列将保留0/1值。

is_deleted = 0 means not deleted

is_deleted = 1 means deleted

<强>步骤:

首先:您需要将列is_deleted添加到您的表格中。

ALTER TABLE your_table ADD is_deleted TINYINT DEFAULT 0;

第二:您需要更新查询才能将此类条目的is_deleted字段状态更新为1

UPDATE 
your_table YT
INNER JOIN
(
    SELECT 
    id,
    class,
    direction,
    test
    FROM your_table
    WHERE results ='Fail'
    GROUP BY id,class,direction,test
    HAVING COUNT(*) >= 1
) AS t 
ON YT.id = t.id AND YT.class = t.class AND YT.direction = t.direction AND YT.test = t.test 
SET is_deleted = 1
WHERE YT.results = 'Pass'

答案 1 :(得分:0)

DELETE passed
    FROM `table` AS passed
INNER JOIN `table` AS failed ON failed.id = passed.id
AND failed.class = passed.class
AND failed.direction = passed.direction
AND failed.test = passed.test
AND failed.results = 'Fail'
WHERE
    passed.results = 'Pass'

但是你应该首先检查一下,如果那些确实是你要删除的结果。所以先使用select。

SELECT passed.* 
FROM
    `table` AS passed
INNER JOIN `table` AS failed ON failed.id = passed.id
AND failed.class = passed.class
AND failed.direction = passed.direction
AND failed.test = passed.test
AND failed.results = 'Fail'
WHERE
    passed.results = 'Pass'

在播放数据之前还要备份数据。

答案 2 :(得分:0)

如果您希望每行有不同的结果,则可以创建一个唯一索引,其中包含id class行,directiontestresults行,您可以防止在第一个位置将这些行插入到表中。

ALTER TABLE `table` 
ADD CONSTRAINT uc_RowUniqueness 
UNIQUE (id,class,direction,test,results)

但是如果你的需要失败,结果是在没有照顾重复记录的情况下插入,那不是解决方案。也许你可以使用触发器。

DELIMITER $$
CREATE TRIGGER delete_similar
BEFORE INSERT ON table
FOR EACH ROW
BEGIN    
    IF NEW.results == 'Pass' THEN
       DELETE FROM table 
           WHERE id=NEW.id AND class=NEW.class 
           AND direction=NEW.direction AND results='Pass'
    END IF;
END $$
DELIMITER ;