如何在Access SQL中执行INTERSECT查询

时间:2014-03-02 06:38:57

标签: ms-access intersect

我有这个Python片段用于查询SQLite表'note','tag'和多对多连接表'fkeys'。我觉得非常程序化,但它确实有效。现在我想在Access中做类似的事情,但Jet SQL不支持INTERSECT

我一直在尝试使用类似帖子的代码几个小时。 WHERE EXISTSGROUP BY ... HAVING +子查询内容都不起作用。 SQL-92选项也没有帮助。可以在Jet SQL中完成吗?

  db.execute('SELECT DISTINCT n.rowid as rowid, n.note_txt as note_txt,\
  date(n.timestamp) as timestamp\
  FROM note n\
  JOIN fkeys f\
  ON n.rowid = f.note_id\
  JOIN tag t\
  ON t.rowid = f.tag_id\
  WHERE t.tag_text = ?\
INTERSECT  \
  SELECT DISTINCT n.rowid as rowid, n.note_txt as note_txt,\
  date(n.timestamp) as timestamp\
  FROM note n\
  JOIN fkeys f\
  ON n.rowid = f.note_id\
  JOIN tag t\
  ON t.rowid = f.tag_id\
  WHERE t.tag_text = ?\
INTERSECT\
  SELECT DISTINCT n.rowid as rowid, n.note_txt as note_txt,\
  date(n.timestamp) as timestamp\
  FROM note n\
  JOIN fkeys f\
  ON n.rowid = f.note_id\
  JOIN tag t\
  ON t.rowid = f.tag_id\
  WHERE t.tag_text = ?\
  ORDER BY timestamp',[srchtxt0,srchtxt1,srchtxt2])

1 个答案:

答案 0 :(得分:2)

我认为以下内容适用于ACE / Jet数据库:

sql = """
SELECT rowid, note_txt, timestamp
FROM [note]
WHERE
    rowid IN
        (
            SELECT f.note_id
            FROM 
                [fkeys] AS f 
                INNER JOIN 
                [tag] AS t 
                    ON t.rowid = f.tag_id 
            WHERE t.tag_text = ?
        )
        AND rowid IN
            (
                SELECT f.note_id
                FROM 
                    [fkeys] AS f 
                    INNER JOIN 
                    [tag] AS t 
                        ON t.rowid = f.tag_id 
                WHERE t.tag_text = ?
            )
        AND rowid IN
            (
                SELECT f.note_id
                FROM 
                    [fkeys] AS f 
                    INNER JOIN 
                    [tag] AS t 
                        ON t.rowid = f.tag_id 
                WHERE t.tag_text = ?
            )
ORDER BY 3 
"""
cursor1 = db.execute(sql,[srchtxt0,srchtxt1,srchtxt2])