Joanne Joanne - 4 months ago 8
MySQL Question

Update a filter in a SQL select code

I want to update the a filter in the following code I wrote. I am confused how to put.

Code is:

$query = "SELECT P.`Building_info` as `name`,P.`Area` as `area`,
avg(
CASE
WHEN P.`Bedroom`='4'
THEN P.`$price`
ELSE NULL
END
) AS 'Beds_4',
avg(
CASE
WHEN P.`Bedroom`='5'
THEN P.`$price`
ELSE NULL
END
) AS 'Beds_5'
FROM All_Rent P
WHERE P.Building_info<>''
AND Sale_Rent='$accom'
AND converted_date > '$min_date'
GROUP BY P.`Building_info`";


I want to add a filter where
Area = "Euston Road"
i.e. select only the areas with the name "Euston Road"

Can anyone help?

Answer

Assuming you're using a variable called $area with a value of "Euston Road", just update your query to this:

$query = "SELECT  P.`Building_info` as `name`,P.`Area` as `area`,
        avg(
            CASE 
                WHEN P.`Bedroom`='4' 
                THEN P.`$price`
                ELSE NULL 
            END
        ) AS 'Beds_4',
        avg(
            CASE 
                WHEN P.`Bedroom`='5' 
                THEN P.`$price`
                ELSE NULL 
            END
        ) AS 'Beds_5'
    FROM    All_Rent P
    WHERE P.Building_info<>''
    AND Sale_Rent='$accom'
    AND converted_date > '$min_date'
    AND P.`Area` = '$area'
    GROUP BY P.`Building_info`";
Comments