mysql select语句中的内连接语法错误

时间:2013-09-02 23:40:36

标签: mysql sql

这是我的MySQL select语句,我试图做一个交集:

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);

来自:Alternative to Intersect in MySQL

但是我在“内部联接”附近遇到语法错误。有谁知道这里有什么不对吗?

1 个答案:

答案 0 :(得分:1)

少数事情:

  1. 您的Where子句在加入后出现 - 这可能是您的问题
  2. 很难理解正在使用的某些字段名称
  3. 尝试稍微修改一下格式并提供其他建议,但这很难理解。
  4. 只是看看你的查询看起来像你用作模板的那个我希望结果是这样的:

    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。也就是说,你可以简化你的查询,看看你是否可以首先使用它 - 你的子选择中有很多条件逻辑,这使得很难在小提琴中进行测试,等等。也许发布一个小提琴作为你的一个例子数据和查询?