0

这是我的 MySQL 选择语句,我正在尝试做一个交集:

SELECT id 
FROM
(SELECT id 
 FROM members WHERE id!=15 AND `last name` = `last name` AND (`first name` = "James") AND `email address` = `email address` AND `mobile number` = `mobile number` AND type_id = type_id AND active = active
) AS A

INNER JOIN

SELECT id 
FROM
(SELECT DISTINCT m.members_id as id 
 FROM map m 
 WHERE m.members_id!=15 AND (((SELECT count(*) FROM tasks) = 0) OR (((SELECT count(*) FROM checklist WHERE map_id=m.id) / (SELECT count(*) FROM tasks)) * 100 >= 0 AND ((SELECT count(*) FROM checklist WHERE map_id=m.id) / (SELECT count(*) FROM tasks)) * 100 <= 100)) AND m.`topic` = m.`topic` AND m.`location` = m.`location` AND m.`country` = m.`country` AND m.`city` = m.`city` AND m.`organization` = m.`organization`
) AS B

USING (id)

这是我看到的示例相交代码

SELECT DISTINCT value FROM table_a
INNER JOIN table_b
USING (value);

来自:MySQL 中相交的替代方案

但是,我在“内部连接”附近遇到语法错误。有谁知道这里有什么问题?

4

1 回答 1

1

一些事情:

  1. 您的 Where 子句出现在连接之后 - 这可能是您的问题
  2. 很难理解您正在使用的某些字段名称发生了什么
  3. 将尝试修复您的格式并提供其他建议,但这很难阅读。

只是看着让您的查询看起来像您用作模板的查询,我希望结果类似于:

SELECT id 
FROM
(SELECT id 
 FROM members WHERE id!=15 AND `last name` = `last name` AND (`first name` = "James") AND `email address` = `email address` AND `mobile number` = `mobile number` AND type_id = type_id AND active = active
) AS A

INNER JOIN

(SELECT DISTINCT m.members_id as id 
 FROM map m 
 WHERE m.members_id!=15 AND (((SELECT count(*) FROM tasks) = 0) OR (((SELECT count(*) FROM checklist WHERE map_id=m.id) / (SELECT count(*) FROM tasks)) * 100 >= 0 AND ((SELECT count(*) FROM checklist WHERE map_id=m.id) / (SELECT count(*) FROM tasks)) * 100 <= 100)) AND m.`topic` = m.`topic` AND m.`location` = m.`location` AND m.`country` = m.`country` AND m.`city` = m.`city` AND m.`organization` = m.`organization`
) AS B

USING (id)

在内部连接之后删除该选择 id。也就是说,您是否可以简化查询并查看是否可以先使其正常工作-您的子选择中有很多条件逻辑,这使得在小提琴中进行测试等变得困难。也许发布一个小提琴作为您的示例数据和查询?

于 2013-09-02T23:46:53.327 回答