我正在尝试从表rem2中获取数据,但未成功。我该怎么做?

SELECT *, rem2.id FROM remuneracao_performance_competencia AS competencia
JOIN remuneracao_performance AS rem1 ON competencia.remuneracao_performance_id = rem1.id
WHERE EXISTS (
    SELECT 1 FROM remuneracao_performance AS rem2 WHERE
        rem1.id <> rem2.id and rem1.cargo_id = rem2.cargo_id AND rem1.frequencia_campanha = rem2.frequencia_campanha AND rem1.modelo_remuneracao = rem2.modelo_remuneracao AND competencia.remuneracao_performance_id <> rem2.id AND rem2.inicio_vigencia_campanha <= competencia.inicio_vigencia and rem2.inicio_vigencia_campanha > rem1.inicio_vigencia_campanha
        ORDER BY rem2.inicio_vigencia_campanha DESC)
ORDER BY cargo_id ASC


错误消息是


  错误代码:1051。“字段列表”中的未知列“ rem2.id”

最佳答案

我将查询更改为:

select distinct competencia.*, rem2.id from remuneracao_performance_competencia as competencia
join remuneracao_performance as rem1 on competencia.remuneracao_performance_id = rem1.id,
     remuneracao_performance as rem2
where
rem1.id <> rem2.id and
rem1.cargo_id = rem2.cargo_id and rem1.frequencia_campanha = rem2.frequencia_campanha and
rem1.modelo_remuneracao = rem2.modelo_remuneracao and competencia.remuneracao_performance_id <> rem2.id and
rem2.inicio_vigencia_campanha <= competencia.inicio_vigencia and
rem2.inicio_vigencia_campanha > rem1.inicio_vigencia_campanha
group by competencia.id, rem1.cargo_id

关于mysql - 如何在exist子句中获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45988443/

10-15 19:00