如何使用MS Access将一个表分成几个?

时间:2012-02-22 09:29:44

标签: sql ms-access-2007

我有一个简单的问题。 我有一张这样的桌子:

ID  NAME     CODE
--  -------  ----
1   JIM      14
1   JOHN     26
2   NICK     26
3   GEORGE   14
4   PAUL     14
4   STEPHEN  26
5   KELLY    14
6   JACK     26

我想从上面创建3个表。一个仅包含代码为14的ID与代码26不具有相同ID的代码。其中一个仅包含代码为26的ID与代码14不具有相同ID。一个包含代码为14的ID和代码26.更确切地说,我想要这些表:

  • 表1:

    ID  NAME    CODE
    --  ------  ----
    3   GEORGE  14
    5   KELLY   14
    
  • 表2:

    ID  NAME  CODE
    --  ----  ----
    2   NICK  26
    6   JACK  26
    
  • 表3:

    ID  NAME     CODE
    --  -------  ----
    1   JIM      14
    1   JOHN     26
    4   PAUL     14
    4   STEPHEN  26
    

提前感谢任何帮助人员!

2 个答案:

答案 0 :(得分:1)

第一张表:

  

CREATE TABLE table1 AS(select * from table3 as T1,其中T1.code = 14   AND T1.id NOT IN(从表3中选择T2.id为T2,其中T1.id = T2.id AND   T2.code = 26));

第二张表:

  

CREATE TABLE table2 AS(select * from table3 as T1,其中T1.code = 26   AND T1.id NOT IN(从表3中选择T2.id为T2,其中T1.id = T2.id AND   T2.code = 14));

第3表:

  

CREATE TABLE table3 AS(从表3中选择T1。*作为T1 INNER JOIN   table3为T2 ON T1.id = T2.id AND((T2.code = 26 AND T1.code = 14)OR   (T2.code = 14 AND T1.code = 26)));

答案 1 :(得分:1)

请记住VIEW是一个表格:

1

CREATE VIEW Table1
AS
SELECT * 
  FROM YourTable AS T1
 WHERE T1.Code = 14
       AND NOT EXISTS (
                       SELECT * 
                         FROM YourTable AS T2
                        WHERE T2.Name = T1.Name 
                              AND T1.Code = 26
                      );

2

CREATE VIEW Table2
AS
SELECT * 
  FROM YourTable AS T1
 WHERE T1.Code = 26
       AND NOT EXISTS (
                       SELECT * 
                         FROM YourTable AS T2
                        WHERE T2.Name = T1.Name 
                              AND T1.Code = 14
                      );

3

CREATE VIEW Table3
AS
SELECT * 
  FROM YourTable AS T1
 WHERE T1.Code = 14
       AND EXISTS (
                   SELECT * 
                     FROM YourTable AS T2
                    WHERE T2.Name = T1.Name 
                          AND T1.Code = 26
                  );

注意CREATE VIEW SQL DDL需要ANSI-92 Query Mode,否则只需按照常规方式将上述内容创建为“查询”对象。