SQL获取WHERE条件顺序

时间:2014-05-04 20:00:05

标签: mysql sql

...

大家好,

我正在尝试用特定顺序显示我用mysql SELECT查询选择的所有数据*,并在一个查询中显示所有这些(或更多但更少更好)。

  • 这个顺序是:首先WHERE条件=需要显示的第一个结果

    第二个条件=第二个结果,直到WHERE条件结束。

http://i.stack.imgur.com/78Uit.png

我希望它很清楚。

这是我的代码:

    $sql= "SELECT O.INTITULE, O.ENTREPRISENOM, O.TYPECONTRAT, O.DATEDEBUT, O.DATEINSERTION, O.REGION,
     O.FONCTION, O.ID_DIFFUSEUR, D.Id, D.MiseEnAvantOffres
    FROM OffreEmploi O join
    Diffuseur D
     on O.ID_DIFFUSEUR = D.Id 
    WHERE (O.INTITULE = ? AND O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1 )
     OR (O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1 ) 
     OR (O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1 ) 
     OR (O.FONCTION = $fonctions AND D.MiseEnAvantOffres = 1 ) 
     OR (O.INTITULE = ? AND O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat ) 
     OR (O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat )";

    $query = $this->db->query($sql,array($_POST['intitule'],$_POST['intitule']));

    if ($query->num_rows() > 0)
    {

       $html = '<div class="resultats">';

       foreach ($query->result_array() as $row)
       {
           $html .= '<p>'.$row['INTITULE'].'</p>';
           $html .= '<p>'.$row['ENTREPRISENOM'].'-'.$row['TYPECONTRAT'].'</p>';
           $html .= '<p>Date de début : '.$row['DATEDEBUT'].'</p>';
           $html .= '<p>Offre publiée le '.$row['DATEINSERTION'].'</p>';
       }

       $html .= '</div>';
    }

    return $html;

}

我希望这些结果能够显示一次,而不是两次或更多次。

我做了几项研究,但我真的不知道该怎么做。

2 个答案:

答案 0 :(得分:1)

使用WHERE运算符组合不同UNION条件给出的结果集。

制作一个额外的列ordBY,并根据决定排序顺序的WHERE条件为其提供值。

SELECT <columnList> , 1 as ordBy
FROM tableName
JOIN<condition>
WHERE <condition1>

UNION

SELECT <columnList> , 2 as ordBy
FROM tableName
JOIN <condition>
WHERE <condition2>
ORDER BY ordBy

注意 您必须仅在最后 ORDER BY条件结束时应用WHERE子句。

答案 1 :(得分:0)

一种方法是使用一个子查询来识别所满足的条件:

SELECT O.INTITULE, O.ENTREPRISENOM, O.TYPECONTRAT, O.DATEDEBUT, O.DATEINSERTION, O.REGION,
       O.FONCTION, O.ID_DIFFUSEUR, D.Id, D.MiseEnAvantOffres,
       (case when (O.INTITULE = ? AND O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1 )
             then 1
             when (O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1 ) 
             then 2
             when (O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1 ) 
             then 3
             when (O.FONCTION = $fonctions AND D.MiseEnAvantOffres = 1 ) 
             then 4
             when (O.INTITULE = ? AND O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat ) 
             then 5
             when (O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat 
             then 6
       end) as whichWhere
FROM OffreEmploi O join
     Diffuseur D
     on O.ID_DIFFUSEUR = D.Id 
HAVING whichWhere is not null
ORDER BY whichWhere;

(注意:这确实会在查询输出中留下排序列。)