如何在INNER JOIN SELECT中使用主选择列

时间:2017-01-16 18:18:18

标签: mysql

SELECT author, excitement, d.*, dist.* FROM profiles as p
INNER JOIN (SELECT id, ABS( excitement - 3.91) as exc_diff from profiles) as d ON p.id = d.id
INNER JOIN (SELECT count(id) as exc_dist from profiles where ABS( excitement - 3.91) < d.exc_diff) as dist;

如何在INNER JOIN SELECT中使用主SELECT列(问题是d.exc_diff,比如说d.exc_diff未知列;)

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT author, excitement, d.*, 
(SELECT count(id) from profiles where ABS( excitement - 3.91) < d.exc_diff) as exc_dist
FROM profiles as p
INNER JOIN (SELECT id, ABS( excitement - 3.91) as exc_diff from profiles) as d ON p.id = d.id;