Syntax error when using UNION

Asked

Viewed 70 times

-2

Error:

/* Erro SQL (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM pedido a,
                        tipofornecimento b
                    ' at line 7 */
/* Affected rows: 0  Registros encontrados: 0  Avisos: 0  Duração de 0 of 1 query: 0,000 sec. */

Consultation:

$sql = "(SELECT 
        a.id,
        a.cliente_id, 
        a.arquiteto_id, 
        a.vendedor_id, 
        b.nome as tipofornecimento,
    FROM {$this->t_pedido} a,
        {$this->t_tipofornecimento} b
    WHERE
            a.fornecimento_id = b.id
        {$where})

        UNION ALL

    (SELECT 
        c.id,
        'a',
        'b',
        'c',
        c.valor as valor
    FROM {$this->t_devolucaoparcelas} c,
        {$this->t_movimentos} d 
    WHERE 
        c.devolucao_id = d.id)
    ";   
  • 3

    Only one caveat the UNION problem is in the SQL database, not PHP

2 answers

1

You have an error in your SQL syntax near 'FROM pedido a, tipofornecimento b ' at line 7

(SELECT   
   a.id,
   a.cliente_id, 
   a.arquiteto_id, 
   a.vendedor_id, 
   b.nome as tipofornecimento, <------- virgula a mais
FROM {$this->t_pedido} a,  

Before the from there is a comma that shouldn’t be there, the error message gives a clue about it.

0

In Union, if I consider that there are no errors in your variables, the fields must have the same name, the same type and the same order in both SQL’s so there are no errors.

(SELECT 
    a.id,
    a.cliente_id, 
    a.arquiteto_id, 
    a.vendedor_id, 
    b.nome as tipofornecimento <----
FROM Pedido a,
    Tipo b
WHERE
    a.fornecimento_id = b.id)

UNION ALL

(SELECT 
    c.id,
    'a',
    'b',
    'c',
    c.valor as valor <----
FROM Devolução c,
    Movimentos d 
WHERE 
    c.devolucao_id = d.id)

I believe the column b.nome does not have the same type as the c.valor. It also had a comma after the b.nome tipofornecimento

Browser other questions tagged

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