mysqli查询的动态排序

时间:2015-06-08 06:21:23

标签: mysql mysqli

我一直试图让这个查询工作一段时间......而我只是不知道如何构建它以使其工作。 WHERE选项工作正常WHERE ".$follow." ".$active." ".$setPub." ".$setLetter." ".$setNumber." ".$setYear." ".$setCond."

但是按选项排序并不是按照我想要的顺序返回项目。

是否构建查询的方式?这会导致问题吗?或者只是查询?我无法看到实际的查询,因为我正在使用mysqli预处理语句。所以我有点卡住了。

if(array_key_exists('option', $sorts) && $sorts['option'] === "low-price-bids")
        {
            //echo "LOW PRICE BIDS";
            $order = "i.item_start, maxbid ASC";
            $sort = "maxbid IS NOT NULL";
        }

if(array_key_exists('option', $sorts) && $sorts['option'] === "low-price-no-bids")
        {
            //echo "LOW PRICE NO BIDS";
            $order = "i.item_start, maxbid ASC";
            $sort = "maxbid IS NULL";
        }

$q = $this->db->mysqli->prepare("SELECT c.ship_cost, 
                c.item_id, 
                i.id, 
                i.user_id, 
                i.item_title, 
                i.item_number, 
                i.item_year, 
                i.item_publisher, 
                i.item_condition, 
                i.item_start, 
                i.item_description, 
                i.active, 
                u.first_name, 
                u.last_name, 
                CAST(u.fb_id AS CHAR(50)) AS fb_id, 
                u.user_pic, 
                MAX(b.bid) AS maxbid, 
                COUNT(b.bid) AS bids, 
                p.publisher_name
                        FROM countries_ship c
                        JOIN items i
                            ON c.item_id = i.id
                        JOIN users u
                            ON i.user_id = u.id
                        LEFT JOIN bids b
                            ON i.id = b.item_id
                        LEFT JOIN publishers p
                            ON i.item_publisher = p.id
                        WHERE ".$follow." ".$active." ".$setPub." ".$setLetter." ".$setNumber." ".$setYear." ".$setCond." ".$sort."
                        GROUP BY i.id
                        ORDER BY ".$order." LIMIT 18");

最高价格回报:

0: {ship_cost: "5", item_id: "624", id: "624", user_id: "62", item_start: "5", maxbid: "",…}
1: {ship_cost: "4", item_id: "623", id: "623", user_id: "62", item_start: "4", maxbid: "",…}
2: {ship_cost: "3", item_id: "622", id: "622", user_id: "296", item_start: "3", maxbid: "10",…}
3: {ship_cost: "2", item_id: "621", id: "621", user_id: "296", item_start: "2", maxbid: "",…}
4: {ship_cost: "0", item_id: "620", id: "620", user_id: "296", item_start: "1", maxbid: "",…}

最低价格:

0: {ship_cost: "0", item_id: "620", id: "620", user_id: "296", item_start: "1", maxbid: "",…}
1: {ship_cost: "2", item_id: "621", id: "621", user_id: "296", item_start: "2", maxbid: "",…}
2: {ship_cost: "3", item_id: "622", id: "622", user_id: "296", item_start: "3", maxbid: "10",…}
3: {ship_cost: "4", item_id: "623", id: "623", user_id: "62", item_start: "4", maxbid: "",…}
4: {ship_cost: "5", item_id: "624", id: "624", user_id: "62", item_start: "5", maxbid: "",…}

0 个答案:

没有答案