幾個小時我一直在處理這個問題。MySQL不存在未知列錯誤
我已經在MySQL的表稱爲person_state
----------
person_state
----------------------------
id | person_id | state | date
----------------------------
我需要得到活動的人最後激活日期,每個人的狀態已經改變了很多次(國家是:主動,被動,等待,阻止)。如果一個人被激活然後停用,我的查詢不應該得到它。
我的查詢是
select id as activation_id, person_id as active_person_id
from person_state
where state = 'active'
and
not exists(
select * from person_state
where person_id = active_person_id
and
id > activation_id
)
我收到錯誤未知列在 'where子句' 'active_person_id'。
感謝您的時間
使用表別名。 – jarlh
我認爲你問的數據庫錯誤的SQL我認爲你需要更多的東西像'GROUP BY person_id HAVING SUM(state ='active')= 1 AND SUM(state ='passive')= 0' ...但是IAM不知道,無法測試它沒有示例數據和預期的輸出 –