-2

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?

Joanne
  • 67
  • 1
  • 9
  • 3
    so add it to the `where` clause? if you don't know how, then you should start learning sql... – Marc B Jul 08 '16 at 21:03
  • You might want to look on how to prevent SQL injection as well: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Christiaan Jul 08 '16 at 21:21

1 Answers1

0

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`";
freakinthesun
  • 699
  • 1
  • 9
  • 19