MySQL - 如何从嵌套选择中删除

时间:2012-08-15 17:22:20

标签: mysql sql

大家好我在这里有我的代码如何进行嵌套选择,现在我的问题是我想删除嵌套选择中的一些文件,但我不知道如何...

SELECT title, 
       filename, 
       jono, 
       client, 
       dt, 
       remarks 
FROM   ((SELECT doc_title    AS title, 
                doc_filename AS filename, 
                doc_jono     AS jono, 
                doc_client   AS client, 
                doc_date     AS dt, 
                remarks 
         FROM   tbl_doc) 
        UNION 
        (SELECT wbok_title    AS title, 
                wbok_filename AS filename, 
                wbok_jono     AS jono, 
                wbok_client   AS client, 
                wbok_date     AS dt, 
                wbok_remarks  AS remarks 
         FROM   tbl_wbok) 
        UNION 
        (SELECT cad_title    AS title, 
                cad_filename AS filename, 
                cad_jono     AS jono, 
                cad_client   AS client, 
                cad_date     AS dt, 
                cad_remarks  AS remarks 
         FROM   tbl_cad) 
        UNION 
        (SELECT oth_title    AS title, 
                oth_filename AS filename, 
                oth_jono     AS jono, 
                oth_client   AS client, 
                oth_date     AS dt, 
                oth_remarks  AS remarks 
         FROM   tbl_oth)) AS t1 
ORDER  BY title 

2 个答案:

答案 0 :(得分:0)

你不能从这个SELECT中做到这一点,因为它包含UNIONs;你必须单独为每个表做DELETE。

顺便说一句,你的四个表应该是一个表。包括包含DOC,WBOK,CAD或OTHER的CATEGORY字段;这样您就可以区分您的四个数据集。这样做将大大简化您的整体逻辑。

答案 1 :(得分:0)

一般形式是

DELETE FROM table tbl
    WHERE tbl.id = (SELECT id FROM [ your query here] )

如果你有桌子

CREATE TABLE Foo (bar integer PRIMARY KEY)

那么这将是

DELETE FROM table tbl
    WHERE tbl.id = (SELECT id FROM table ... JOIN ..  WHERE X=y etc.. )

如果您的情况如果要删除UNION中的内容,则必须逐个删除,因为您只能删除 一个表一次。

所有表格的数据结构都相同。 Robert Harvey建议您创建一个名为“CATEGORY”的附加列的单个表,该列对数据所用的表格进行编码。这一般是一个好主意,它会使您的删除更加容易