通过PHP加入表和订单?

时间:2015-03-11 13:59:34

标签: php mysql

我正在尝试使用php从最低到最高,从最高到最低做一个简单的顺序无效。

我不知道是什么让它停止工作,我总是将mysql数据ordered by yt.id从最低点到最高点出于某种奇怪的原因。

这是我的查询代码:

$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
    if(isset($_POST['keyword'])){

        foreach($_POST['keyword'] as $c){
            if(!empty($c)){
                $keyword2 = $_POST['keyword2'];
                ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
                $sql .= $clause . " (ATTRIBUTES.sizes LIKE BINARY '$c' OR ATTRIBUTES.colors LIKE BINARY '$c' OR ATTRIBUTES.manu LIKE BINARY '$c' OR ATTRIBUTES.attr LIKE BINARY '$c') AND ATTRIBUTES.type='$keyword2'";
                $clause = " OR ";//Change  to OR after 1st WHERE
            }
        }
        $sql .= " GROUP BY yt.id";
        $sql .= " ORDER BY yt.id DESC";

///rest of my code/////

我做什么并不重要,我总是从最低到最高!

我是这样做的吗?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

你总是有$_POST['keyword']吗?

如果没有,或者即使您尝试过:

$clause = " WHERE ";//Initial clause
$sql="SELECT *
FROM `yt`
INNER JOIN `ATTRIBUTES` ON yt.id=ATTRIBUTES.id";//Query stub
if(isset($_POST['keyword'])){

    foreach($_POST['keyword'] as $c){
        if(!empty($c)){
            $keyword2 = $_POST['keyword2'];
            ##NOPE##$sql .= $clause."`".$c."` LIKE '%{$c}%'";
            $sql .= $clause . " (ATTRIBUTES.sizes LIKE BINARY '$c' OR ATTRIBUTES.colors LIKE BINARY '$c' OR ATTRIBUTES.manu LIKE BINARY '$c' OR ATTRIBUTES.attr LIKE BINARY '$c') AND ATTRIBUTES.type='$keyword2'";
            $clause = " OR ";//Change  to OR after 1st WHERE
        }
    }
}
$sql .= " GROUP BY yt.id";
$sql .= " ORDER BY yt.id DESC";

或者只是向我们展示一个完整的代码片段,如果没有或在if(isset($_POST['keyword'])){

之后会发生什么