无法通过Case获得在Mysql中工作

时间:2017-04-26 18:18:01

标签: php mysql

我看到了另一个问题的答案,这就是我如何开始,但我无法让我的代码工作。我认为它与(Price>'0.00')有关,因为mysql一直给我这个错误,

“#1064 - 您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在'ORDER BY Price = 0,Price asc,Style asc,Manufacturer asc END WHEN附近使用正确的语法((价格< ='在第8行“。

<?php 
$order = mysqli_query($con, "SELECT DivisionOf, Manufacturer, Style, Include,
Fiber, Width, Backing
FROM CarpetInfo

WHERE (DivisionOf='Mohawk') AND (Manufacturer IN ('Aladdin'))
AND CASE
WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes')
THEN ORDER BY Price = 0, Price asc, Style asc, Manufacturer asc
END

WHEN ((Price <= '0.00') OR (Price > '49.95') OR (ShowPrice!='Yes'))
THEN ORDER BY Price = 0, Style asc, Manufacturer asc
END
");
$result = mysqli_fetch_row($order);

mysqli_data_seek($order, 0);
while($info = mysqli_fetch_array( $order ))
include($_SERVER['DOCUMENT_ROOT'].$info['Include']);?>

1 个答案:

答案 0 :(得分:0)

跟进我的评论。

  1. 您无法将整个ORDER BY条款放入CASE声明中
  2. ENDCASE是允许的(在案例陈述的最后)
  3. 相反,以下内容应该有效:

    SELECT DivisionOf, Manufacturer, Style, Include,Fiber, Width, Backing
    FROM CarpetInfo
    WHERE (DivisionOf='Mohawk') AND (Manufacturer IN ('Aladdin'))
    ORDER BY
        /*sort field 1*/
        CASE
            WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes')
                THEN Price
            WHEN ((Price <= '0.00') OR (Price > '49.95') OR (ShowPrice!='Yes'))
                THEN Style
        END asc,
        /*sort field 2*/
        CASE
            WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes')
                THEN Style
            WHEN ((Price <= '0.00') OR (Price > '49.95') OR (ShowPrice!='Yes'))
                THEN Manufacturer
        END asc,
        /*sort field 3*/
        CASE
            WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes')
                THEN Manufacturer       
        END asc;
    

    这有点难看,因为你只能从case语句中返回一个字段,然后在你END之后的那个ORDER BY字段的情况下指定方向。

    另外,虽然我不熟悉您的数据。我认为可以使用CASE简化这些ELSE语句:

    SELECT DivisionOf, Manufacturer, Style, Include,Fiber, Width, Backing
    FROM CarpetInfo
    WHERE (DivisionOf='Mohawk') AND (Manufacturer IN ('Aladdin'))
    ORDER BY
        CASE WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes') THEN Price ELSE Style END asc,
        CASE WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes') THEN Style ELSE Manufacturer asc,
        CASE WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes') THEN Manufacturer END asc;
    

    更新:因为我们正在混合字段类型,所以在某些情况下,您最终会使用词典排序而不是数字。一些放在案例陈述中的人应该有所帮助:

    SELECT DivisionOf, Manufacturer, Style, Include,Fiber, Width, Backing
    FROM CarpetInfo
    WHERE (DivisionOf='Mohawk') AND (Manufacturer IN ('Aladdin'))
    ORDER BY
        CASE WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes') THEN Price ELSE 0 END asc,
        CASE WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes') THEN NULL ELSE Style END asc,
        CASE WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes') THEN Style ELSE Manufacturer asc,
        CASE WHEN (Price > '0.00') AND (Price <= '49.95') AND (ShowPrice='Yes') THEN Manufacturer END asc;
    

    所以现在第一个要点击的是price中的值或0,然后style被移动到第二个排序顺序。你不得不四处闲逛,让它随你工作。