MYSQL查询语法 - 未知列错误但列存在

时间:2013-03-06 13:24:37

标签: mysql sql

此查询有什么问题:

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'

我明白了 -

  

'where子句'中的未知列'hst.new_value'

。 但该专栏存在!!!

2 个答案:

答案 0 :(得分: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 :(得分: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提到的查询相同。