简体   繁体   中英

#1054 -unknown column in order clause after exporting to MariaDB from Mysql

I'm getting this #1054 -unknown column in order clause error, after I export my database from one hosting with MySql 5.7.23-cll-lve to another hosting with MariaDB 10.2.27-MariaDB-cll-lve. I have both hosting working right now so I can see that the queries work in the first one and in the MariaDB throws this error. And I checked that the column exists, the structure is the same. I also try to export using "MYSQL40", but nothing changed.

What can I do?

I will put the query that is giving the problem (is not the only one, but the others are similar):

SELECT * FROM (
    (SELECT result.*, IFNULL(SUM(mv.monto_pago_fv), 0) AS pago  FROM(
        (SELECT DISTINCT f.id_factura_de_venta, f.numero_factura_de_venta, f.fecha_contable_fv, f.fecha_fv, f.anular_fv, 
            f.id_cliente, f.tipo,
            c.rut, c.nombre, SUM(fot.monto_pago_ot) AS monto_facturado_neto, SUM(ROUND(fot.monto_pago_ot*0.19)) AS iva
        FROM facturas_de_ventas f
        JOIN clientes_y_proveedores c ON f.id_cliente=c.id_cliente_o_proveedor
        JOIN facturas_de_ot fot ON f.id_factura_de_venta=fot.id_factura_de_venta
        GROUP BY f.id_factura_de_venta
        ORDER BY f.id_factura_de_venta DESC)
        UNION
        (SELECT id_factura_de_venta, numero_factura_de_venta, fecha_contable_fv, fecha_fv, anular_fv,
                id_cliente, tipo, rut, nombre, SUM(monto_facturado_neto) AS monto_facturado_neto, SUM(iva) as iva  FROM(
            (SELECT DISTINCT f.*, c.rut, c.nombre, SUM(s.valor_sf*s.cantidad_sf) AS monto_facturado_neto, 
            SUM(ROUND(s.valor_sf*s.cantidad_sf*0.19)) as iva
            FROM facturas_de_ventas f
            JOIN clientes_y_proveedores c ON f.id_cliente=c.id_cliente_o_proveedor
            JOIN servicios_factura s ON f.id_factura_de_venta=s.id_factura_de_venta
            GROUP BY f.id_factura_de_venta)
            UNION
            (SELECT DISTINCT f.*, c.rut, c.nombre, SUM(mf.valor_mf*mf.cantidad_mf) AS monto_facturado_neto, SUM(IF(m.iva_material=1, mf.valor_mf*mf.cantidad_mf*0.19, 0)) AS iva
            FROM facturas_de_ventas f
            JOIN clientes_y_proveedores c ON f.id_cliente=c.id_cliente_o_proveedor
            JOIN materiales_factura mf ON f.id_factura_de_venta=mf.id_factura_de_venta
            JOIN materiales m ON mf.id_material=m.id_material
            GROUP BY f.id_factura_de_venta)
            ) AS fms
        GROUP BY id_factura_de_venta)
    ) AS result
    LEFT JOIN montos_pago_v mv ON result.id_factura_de_venta=mv.id_factura_de_venta
    GROUP BY result.id_factura_de_venta)
    UNION ALL
    (SELECT DISTINCT f.id_factura_de_venta, ncv.numero_ncv, ncv.fecha_contable_ncv, ncv.fecha_ncv, 
    f.anular_fv, f.id_cliente, 'Nota crédito' AS tipo, c.rut, c.nombre,
    ncv.valor_descontado_ncv AS total_neto, ROUND(ncv.valor_descontado_ncv*0.19) AS iva, SUM(IFNULL(m.monto_pago, 0)) AS pago
    FROM notas_credito_ventas ncv 
    JOIN facturas_de_ventas f ON ncv.id_factura_de_venta=f.id_factura_de_venta AND f.anular_fv=0
    JOIN clientes_y_proveedores c ON f.id_cliente=c.id_cliente_o_proveedor 
    LEFT JOIN montos_pago_ncv m ON ncv.id_nota_credito_venta=m.id_nota_credito_venta
    WHERE ncv.valor_descontado_ncv != 0
    GROUP BY ncv.id_nota_credito_venta
    ORDER BY ncv.id_nota_credito_venta) 
)AS r
WHERE tipo!='boleta' AND MONTH(fecha_contable_fv) =11 AND YEAR(fecha_contable_fv) =2019;

I checked and the problem is in the final WHERE clause, specifically after "tipo,='boleta'", because I prove all the other queries in parenthesis individually and they work, and I also erase the part "AND MONTH(fecha_contable_fv) =11 AND YEAR(fecha_contable_fv) =2019" and in that case it works in the hosting with MariaDB. I repeat that that same query works perfectly in the other hosting with Mysql, So the problem should be some option in the new hosting, or maybe the way of exporting. or maybe I should correct a lot of queries now that I am using MariaDB. I hope someone can help me. Thanks in advance.

Can't you move the tests on tipo and fecha_contable_fv inside? That is do the filtering before doing all the UNIONing and GROUPing .

Here's a way to do the date check in a single step (no faster, just more concise):

fecha_contable_fv LIKE '2019-11%'

If there were a usable index, I would recommend:

    fecha_contable_fv >= '2019-11-01'
AND fecha_contable_fv  < '2019-11-01' + INTERVAL 1 MONTH

What was the full error message; didn't it say more than just "#1054 -unknown column in order clause error"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM