访问

时间:2017-01-11 16:08:31

标签: sql database ms-access

我正在运行一个简单的查询。我想获取在两个表中找到的记录,但是我得到语法错误。 (FROM子句中的语法错误)我在我的sql server 2008中运行此查询,它运行顺利,但如果我在访问db中执行此操作,则会出现此语法错误

从Billing0923中选择DISTINCT * 相交 选择DISTINCT *来自Billing916

2 个答案:

答案 0 :(得分:0)

再次有人坚持使用Access ......但不要绝望,Hive有同样的问题,他们在GitHub中有一些用户定义的函数可以解决它。

对于两个设置操作MINUS / EXCEPT或者实际上是INTERSECT,您将不得不求助于一个表的SELECT DISTINCT a。*作为另一个表AS b的INNER JOIN-ed,其中所有列都是ON子句中的两个表。丑,我知道......

CREATE TABLE work ( 
  division         INT
, fips_county_code VARCHAR(3)
, last             VARCHAR(10)
, suffix           VARCHAR(3)
, first            VARCHAR(6)
, title            VARCHAR(16) 
, birthdate        DATE
);

INSERT INTO work VALUES(1,'UK' ,'Dent'      ,'','Arthur','Earthling'       ,'1957-08-02');
INSERT INTO work VALUES(1,'UK' ,'Prefect'   ,'','Ford'  ,'HH Guide Scout'  ,'1925-04-01');
INSERT INTO work VALUES(1,'N/A','Beeblebrox','','Zaphod','Galaxy President','1857-09-13');
INSERT INTO work VALUES(1,'UK' ,'McMillan'  ,'','Tricia','Astrophysicist'  ,'1959-09-29');
INSERT INTO work VALUES(1,'UK' ,'Dent'      ,'','Arthur','Earthling'       ,'1957-08-02');
INSERT INTO work VALUES(1,'UK' ,'McMillan'  ,'','Tricia','Astrophysicist'  ,'1959-09-29');

CREATE TABLE work_2 ( 
  division         INT
, fips_county_code VARCHAR(3)
, last             VARCHAR(10)
, suffix           VARCHAR(3)
, first            VARCHAR(6)
, title            VARCHAR(16) 
, birthdate        DATE
);
INSERT INTO work_2 VALUES(1,'UK' ,'Dent'      ,'','Arthur','Earthling'          ,'1957-08-02');
INSERT INTO work_2 VALUES(1,'UK' ,'Prefect'   ,'','Ford'  ,'HH Guide Scout'     ,'1925-04-01');
INSERT INTO work_2 VALUES(1,'UK' ,'Prosser'   ,'','Edwin' ,'Bulldozer Team Head','1957-08-02');
INSERT INTO work_2 VALUES(1,'N/A','Halfrunt'  ,'','Gag'   ,'Musician'           ,'1925-04-01');

SELECT DISTINCT
  work.*
FROM work AS work
INNER JOIN work_2 AS cmp
   ON work.division         = cmp.division
  AND work.fips_county_code = cmp.fips_county_code
  AND work.last             = cmp.last
  AND work.suffix           = cmp.suffix
  AND work.first            = cmp.first
  AND work.title            = cmp.title
  AND work.birthdate        = cmp.birthdate
;

division|fips_county_code|last   |suffix|first |title         |birthdate
       1|UK              |Dent   |      |Arthur|Earthling     |1957-08-02 00:00:00
       1|UK              |Prefect|      |Ford  |HH Guide Scout|1925-04-01 00:00:00

快乐玩

答案 1 :(得分:0)

使用UNION

SELECT * FROM Billing0923
UNION
SELECT * FROM Billing916

假设您的两个表具有完全相同的字段。否则,请列出他们共有的字段,而不是使用*