根据mysql查询中的列值过滤行

时间:2014-08-06 12:49:17

标签: mysql sql

这是我的mysql查询

SELECT a.model_name, 
       IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year, 
       IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month 
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b 
WHERE a.model_id = b.model_id AND a.model_status = '1' 
ORDER BY a.model_created_on DESC

我想在此查询中再做一个过滤选项。我需要根据release_year = 1&获取记录。 release_year = 1.我通过MYSQL QUERY中的IF STATEMENT完成了release_year和release_month列

release_year

IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year

RELEASE_YEAR年

IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month

如何在此查询中根据这些值(release_month = 1& release_year = 1)获取记录?我试过了WHERE release_month = 1 AND release_year = 1,但这个回复了未知列

1 个答案:

答案 0 :(得分:2)

你可以这样做:

SELECT
    *
FROM
(
SELECT 
    a.model_name,
    a.model_created_on, 
    IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year, 
    IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month 
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b 
WHERE a.model_id = b.model_id AND a.model_status = '1' 

) AS tbl
WHERE tbl.release_year=1 AND release_month=1
ORDER BY tbl.model_created_on DESC