这是我的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
但是我在“内部联接”附近遇到语法错误。有谁知道这里有什么不对吗?
答案 0 :(得分:1)
少数事情:
只是看看你的查询看起来像你用作模板的那个我希望结果是这样的:
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。也就是说,你可以简化你的查询,看看你是否可以首先使用它 - 你的子选择中有很多条件逻辑,这使得很难在小提琴中进行测试,等等。也许发布一个小提琴作为你的一个例子数据和查询?