Syntax error when using UNION [closed]

-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. */

Inquiry:

$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)
    ";   
    
asked by anonymous 26.10.2015 / 18:32

2 answers

1

You have an error in your SQL syntax near 'FROM order to, type supply 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 from there is a comma that should not be there, the error message gives a clue about it.

    
26.10.2015 / 18:51
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 the two SQL so that 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 b.nome column does not have the same type as c.valor . Also had a comma after% w / o%

    
26.10.2015 / 18:47