簡體   English   中英

SQL 選擇多個“類別”或“類型”時查詢不返回結果

[英]SQL query not returning results when selecting multiple "category" or "type"

我正在嘗試實現 SQL 查詢。 從 html 搜索表單中選擇一個類別或類型時,它可以正常工作,但是一旦我 select 超過一個類別或類型,它不會返回任何結果(盡管數據庫包含這些條目)。 有人可以建議構造查詢的正確方法是什么?

    public static function paginatesearch($location, $bedrooms, $bathrooms, $minamount, $maxamount, $forsale, $torent, $holidayaccommodation, $house, $apartment, $duplex, $simplex, $lodge, $campsite, $hotel, $commercial, $industrial, $land, $sortby)
    {
        $db = static::getDB();

        $sql = 'SELECT *,

        posts.id as postId,
        users.id as userId,
        posts.created_at as postCreated,
        users.created_at as userCreated
        FROM posts
        INNER JOIN users
        ON posts.user_id = users.id

        WHERE coverimage != "default.jpg"
        
        AND location = :location';
        

        if ($bedrooms != '') {
            $sql .= ' AND bedrooms = :bedrooms';
        }
        if ($bathrooms != '') {
            $sql .= ' AND bathrooms = :bathrooms';
        }
        if ($minamount != '') {
            $sql .= ' AND amount >= :minamount';
        }
        if ($maxamount != '') {
            $sql .= ' AND amount <= :maxamount';
        }

        if ($forsale != '') {
            $sql .= ' AND category = :forsale';
        }

        if ($torent != '') {
            $sql .= ' AND category = :torent';
        }

        if ($holidayaccommodation != '') {
            $sql .= ' AND category = :holidayaccommodation';
        }


        if ($house != '') {
            $sql .= ' AND type = :house';
        }

        if ($apartment != '') {
            $sql .= ' AND type = :apartment';
        }
        if ($duplex != '') {
            $sql .= ' AND type = :duplex';
        }
        if ($simplex != '') {
            $sql .= ' AND type = :simplex';
        }
        if ($lodge != '') {
            $sql .= ' AND type = :lodge';
        }
        if ($campsite != '') {
            $sql .= ' AND type = :campsite';
        }
        if ($hotel != '') {
            $sql .= ' AND type = :hotel';
        }
        if ($commercial != '') {
            $sql .= ' AND type = :commercial';
        }
        if ($industrial != '') {
            $sql .= ' AND type = :industrial';
        }
        if ($land != '') {
            $sql .= ' AND type = :land';
        }


        if ($sortby == 'newest') {
            $sql .="\nORDER BY posts.created_at DESC";
        }

        if ($sortby == 'oldest') {
            $sql .="\nORDER BY posts.created_at ASC";
        }

        if ($sortby == 'highest') {
            $sql .= "\nORDER BY posts.amount DESC";
        }

        if ($sortby == 'lowest') {
            $sql .= "\nORDER BY posts.amount ASC";
        }


        $stmt = $db->prepare($sql);
     
        $stmt->bindValue(':location', $location, PDO::PARAM_STR);
    
        if ($bedrooms != '') {
            $stmt->bindValue(':bedrooms', $bedrooms, PDO::PARAM_INT);
        }
        if ($bathrooms != '') {
            $stmt->bindValue(':bathrooms', $bathrooms, PDO::PARAM_INT);
        }
        if ($minamount != '') {
            $stmt->bindValue(':minamount', $minamount, PDO::PARAM_INT);
        }
        if ($maxamount != '') {
            $stmt->bindValue(':maxamount', $maxamount, PDO::PARAM_STR);
        }

        if ($forsale != '') {
            $stmt->bindValue(':forsale', $forsale, PDO::PARAM_STR);
        }
        if ($torent != '') {
            $stmt->bindValue(':torent', $torent, PDO::PARAM_STR);
        }

        if ($holidayaccommodation != '') {
            $stmt->bindValue(':holidayaccommodation', $holidayaccommodation, PDO::PARAM_STR);
        }
      
        if ($house != '') {
            $stmt->bindValue(':house', $house, PDO::PARAM_STR);
        }

        if ($apartment != '') {
            $stmt->bindValue(':apartment', $apartment, PDO::PARAM_STR);
        }
        if ($duplex != '') {
            $stmt->bindValue(':duplex', $duplex, PDO::PARAM_STR);
        }
        if ($simplex != '') {
            $stmt->bindValue(':simplex', $simplex, PDO::PARAM_STR);
        }
        if ($lodge != '') {
            $stmt->bindValue(':lodge', $lodge, PDO::PARAM_STR);
        }
        if ($campsite != '') {
            $stmt->bindValue(':campsite', $campsite, PDO::PARAM_STR);
        }
        if ($hotel != '') {
            $stmt->bindValue(':hotel', $hotel, PDO::PARAM_STR);
        }
        if ($commercial != '') {
            $stmt->bindValue(':commercial', $commercial, PDO::PARAM_STR);
        }
        if ($industrial != '') {
            $stmt->bindValue(':industrail', $industrial, PDO::PARAM_STR);
        }
        if ($land != '') {
            $stmt->bindValue(':land', $land, PDO::PARAM_STR);
        }

        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }


多個類別不起作用的原因是因為您將它們與“and”組合,但您應該將它們與“or”組合

目前你最終得到where type = 'simplex' AND type = 'lodge'
你應該有where (type = 'simplex' OR type = 'lodge')'

“或”語句應包含在( )

另一種解決方案是將類別作為數組傳遞並使用 IN 語句, where type in ('lodge', 'simplex')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM