0

I'm currrently stuck with my sql query.

I want to do multiple select query but all in one. Each select is for the same table, same column but have different conditions.

I'm using this for the moment :

    /* Ordre d'affichage 1 */
    $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, Diffuseur D WHERE O.INTITULE = ? AND O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND O.ID_DIFFUSEUR = D.Id AND D.MiseEnAvantOffres = 1 ";
    /* Ordre d'affichage 2 */
    $sql .= "EXCEPT ";
    $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, Diffuseur D WHERE O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND O.ID_DIFFUSEUR = D.Id AND D.MiseEnAvantOffres = 1 ";
    /* Ordre d'affichage 3 */
    $sql .= "EXCEPT ";
    $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, Diffuseur D WHERE O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND O.ID_DIFFUSEUR = D.Id AND D.MiseEnAvantOffres = 1 ";
    /* Ordre d'affichage 4 */   
    $sql .= "EXCEPT ";
    $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, Diffuseur D WHERE O.FONCTION = $fonctions AND O.ID_DIFFUSEUR = D.Id AND D.MiseEnAvantOffres = 1 ";
    /* Ordre d'affichage 5 */
    $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, Diffuseur D WHERE O.INTITULE = ? AND O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat ";
    $sql .= "EXCEPT ";
    /* Ordre d'affichage 6 */
    $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, Diffuseur D WHERE O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat ";

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

Is there a way to make this easier and possible? (because currently the except does not work)

3
  • 2
    Have you considered UNION? Commented May 2, 2014 at 10:22
  • Still struggling? Consider providing proper DDLs (and/or an sqlfiddle) TOGETHER WITH THE DESIRED RESULT SET. Commented May 2, 2014 at 10:23
  • UNION didn't work too, will try NOT IN, thx Commented May 2, 2014 at 10:25

1 Answer 1

2

Why can't you do this as one long where clause:

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) and
      not (O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1) and
      not (O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat AND D.MiseEnAvantOffres = 1) and
      not (O.FONCTION = $fonctions AND D.MiseEnAvantOffres = 1) and
      not (O.INTITULE = ? AND O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat) and
      not (O.REGION = $regions AND O.FONCTION = $fonctions AND O.TYPECONTRAT = $type_contrat);

The last two are missing the (implicit) join conditions in the where clauses. If you were having problems, that could have been the cause. You should learn to always use explicit joins.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.