从表中删除

时间:2015-07-06 10:54:11

标签: mysql sql

我有一张表,其中最初有以下详细信息

+-------+--------------+------------+-------+
| T_KEY |    T_DATE    | T_TEN      | T_MEM |
+-------+--------------+------------+-------+
|    1  |  0000-00-00  | a          |    0  |
|    2  |  0000-00-00  | b          |    0  |
|    3  |  0000-00-00  | c          |    0  |
|    4  |  0000-00-00  | d          |    0  |
|    5  |  0000-00-00  | e          |    0  |
|    6  |  0000-00-00  | f          |    0  |
+-------+--------------+------------+-------+

插入后,表格如下所示

+-------+--------------+-----------+--------+
| T_KEY |    T_DATE    | T_TEN     |  T_MEM |
+-------+--------------+-----------+--------+
|    1  | 0000-00-00   | a         |     0  |
|    2  | 0000-00-00   | b         |     0  |
|    3  | 0000-00-00   | c         |     0  |
|    4  | 0000-00-00   | d         |     0  |
|    5  | 0000-00-00   | e         |     0  |
|    6  | 0000-00-00   | f         |     0  |
|    7  | 2015-01-01   | a         |     1  |
|    8  | 2015-01-01   | b         |     3  |
|    9  | 2015-01-01   | c         |     5  |
|    10 | 2015-01-02   | a         |     5  |
|    11 | 2015-01-02   | b         |     8  |
|    12 | 2015-01-02   | c         |     1  |
+-------+--------------+-----------+--------+

在插入操作期间,对于T_TEN = a,b,在T_DATE&中插入了非零值。 T_MEM字段。在插入操作之后,我想运行一个清理操作(删除查询),它应该识别每个都有一行数据的所有T_TEN。 T_DATE =' 0000-00-00'和T_MEM = 0

T_DATE ="' 0000-00-00'"
以外的任何值 和
T_MEM ="除零以外的任何值"

一旦确定,它应该删除包含T_DATE =' 0000-00-00'对于那个特定的T_TEN,T_MEM = 0。

在上面的示例中,在插入T_TEN = a之后,更新后的表有3个条目,用于' a'

+-------+--------------+-----------+--------+
| T_KEY |    T_DATE    | T_TEN     |  T_MEM |
+-------+--------------+-----------+--------+
|    1  |  0000-00-00  | a         |     0  |
|    7  |  2015-01-02  | a         |     1  |
|    10 |  2015-01-02  | a         |     5  |

我希望摆脱T_KEY = 1的行,因为T_TEN = a,现在同时具有零和&字段T_DATE&的非零值。 T_MEM。

同样,对于T_TEN = b& c,我想用T_DATE =' 0000-00-00'删除行。并且T_MEM = 0,因为插入后这些字段的条目非零。

必需的输出

+-------+-------------+-------------+-------+
| T_KEY |    T_DATE   |  T_TEN      | T_MEM |
+-------+-------------+-------------+-------+
|    4  | 0000-00-00  | d           |    0  |
|    5  | 0000-00-00  | e           |    0  |
|    6  | 0000-00-00  | f           |    0  |
|    7  | 2015-01-01  | a           |    1  |
|    8  | 2015-01-01  | b           |    3  |
|    9  | 2015-01-01  | c           |    5  |
|   10  | 2015-01-02  | a           |    5  |
|   11  | 2015-01-02  | b           |    8  |
|   12  | 2015-01-02  | c           |    1  |
+-------+-------------+-------------+-------+

INSERT SQL我使用的是

INSERT INTO 
        databse_2.th_sc(T_DATE,T_TEN,T_MEM)     
            SELECT  
                        ms1.M_DATE,
                        ms1.M_TEN, 
                        CASE
                            WHEN SUM(ms1.MEM_US) BETWEEN 1 AND 3 THEN 1
                            WHEN SUM(ms1.MEM_US) BETWEEN 3 AND 5 THEN 3
                            WHEN SUM(ms1.MEM_US) BETWEEN 5 AND 10 THEN 5
                            WHEN SUM(ms1.MEM_US) BETWEEN 20 AND 30 THEN 8
                            WHEN SUM(ms1.MEM_US) BETWEEN 30 AND 50 THEN 10
                            WHEN SUM(ms1.MEM_US) > 50 THEN 15
                            ELSE 0
                        END AS MEM_TI
            FROM
                        database_1.MEM_SW ms1
            WHERE 
                        ms1.MEM_IN IN ( 
                             SELECT 
                                    tw2.T_NAME
                             FROM 
                                     database_1.T_WY tw2
                        ) 
                        AND ms1.M_DATE between '2015-05-01' and '2015-06-01' 
                        AND ms1.MEM_IN LIKE 'ks%ny%'  
            GROUP BY 
                        ms1.M_DATE,
                        ms1.MEM_TEN;

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

从t_date和t_mem为空的表中删除但是存在具有相同t_ten的记录,其中t_date和t_mem都不为空。使用EXISTS或IN:

delete 
from mytable
where t_date = '0000-00-00' and t_mem = 0
and t_ten in
(
  select t_ten
  from mytable
  where not (t_date = '0000-00-00' and t_mem = 0)
);

答案 1 :(得分:0)

克服错误的诀窍"对不起之前的错误更新感到抱歉。我运行SQL时遇到以下错误 - MySQL说:文档#1093 - 您无法指定目标表' mytable'用于FROM子句中的更新 - "

是用SELECT包装内部SELECT。 (http://dasunhegoda.com/1093-you-cant-specify-target-table-table_name-for-update-in-from-clause/104/

 DELETE 
    FROM  
            mytable
    WHERE 
            t_date = '0000-00-00'
            AND t_ten IN (
                SELECT * FROM (
                    SELECT
                            t_ten
                    FROM 
                            mytable
                    WHERE 
                            NOT (t_date = '0000-00-00')
                ) AS  XY
            );