How to use except in mysql

Asked

Viewed 1,143 times

0

I need to select one in an offer table and then use an except to delete the other select result

Example:

A = {10,11,12} B = {11,12} (A except B) = {10}

I’m talking about except pq for what I had searched for, sure the code below in sql server but I wanted a similar code in mysql or if possible it can be something in php

SELECT ofe.ofeCodigo, ofe.ofeTitulo FROM tbloferta ofe, tblcategoriaoferta cof WHERE ofe.cofCodigo = cof.cofCodigo AND ofe.ofeStatus = 'Aberto' AND cof.cofDescricao = 'Compra'
EXCEPT
SELECT ofe.ofeCodigo, ofe.ofeTitulo FROM tbloferta ofe, tblofertanegociacao ofeneg, tblcategoriaoferta cof WHERE ofe.ofeCodigo = ofeneg.ofeCodigo AND ofe.cofCodigo = cof.cofCodigo AND ofeneg.negCodigo = 40 AND cof.cofDescricao = 'Compra'
  • I believe that Mysql does not implement the combination operations of EXCEPT and INTERSECT queries, only UNION.

1 answer

2


I believe that the EXCEPT does not work in the Mysql, but you can try using the NOT IN. For example:

SELECT 
    ofe.ofeCodigo, 
    ofe.ofeTitulo 
    FROM 
        tbloferta ofe, 
        tblcategoriaoferta cof 
    WHERE 
        ofe.cofCodigo = cof.cofCodigo AND 
        ofe.ofeStatus = 'Aberto' AND 
        cof.cofDescricao = 'Compra' AND
        ofe.ofeCodigo NOT IN (
            SELECT ofe.ofeCodigo 
            FROM 
                tbloferta ofe, 
                tblofertanegociacao ofeneg, 
                tblcategoriaoferta cof 
            WHERE 
                ofe.ofeCodigo = ofeneg.ofeCodigo AND 
                ofe.cofCodigo = cof.cofCodigo AND 
                ofeneg.negCodigo = 40 AND 
                cof.cofDescricao = 'Compra'
        )
  • Thank you very much, that’s just what I wanted, an alternative to sql server EXCEPT in mysql, it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.