2013-03-06 62 views
0

有什麼不對這個查詢:MySQL查詢語法 - 未知列的錯誤,但列中存在

SELECT * 
FROM families f JOIN family_histories fh ON f.id = fh.family_id 
WHERE f.family_status_cat_id = 1422 
AND (SELECT hst.created_at 
    FROM family_histories hst 
    WHERE hst.family_history_cat_id = 1422 
    AND hst.new_value LIKE '%1422%') 
BETWEEN '2010/2/13' AND '2013/3/6' 

我得到 -

未知列 'hst.new_value' 在 'where子句'

。 但列存在!

+2

這只是意味着'family_histories'沒有名爲'new_value'的列 – 2013-03-06 13:25:38

+0

如果刪除條件「AND hst.new_value LIKE'%1422%'」它會給你什麼錯誤? – DevelopmentIsMyPassion 2013-03-06 13:28:00

+1

你的語法不正確......連接的表和子句'between'沒有連接 – Justin 2013-03-06 13:30:25

回答

1

嘗試此查詢 -

SELECT * FROM families f 
    JOIN family_histories fh 
    ON f.id = fh.family_id 
WHERE 
    f.family_status_cat_id = 1422 AND 
    fh.created_at BETWEEN '2010-02-13' AND '2013-03-06' AND 
    fh.family_history_cat_id = 1422 AND 
    fh.new_value LIKE '%1422%' 

這是你想要的嗎?

1

如果列名是new_val,那麼你應該有下面的查詢

SELECT * FROM families f 
JOIN family_histories fh 
ON f.id = fh.family_id 
WHERE 
f.family_status_cat_id = 1422 AND 
fh.created_at BETWEEN '2010-02-13' AND '2013-03-06' AND 
fh.family_history_cat_id = 1422 AND 
fh.new_val LIKE '%1422%' 

你不會需要第二個選擇,你在查詢中有當前。它與Devart提到的查詢相同。