Fix “ERROR 1222 (21000): The used SELECT statements have a different number of columns” when using UNION in MariaDB

When using the UNION operator in MariaDB, you may encounter the following error: “ERROR 1222 (21000): The used SELECT statements have a different number of columns”.

This error occurs when the number of columns returned by each SELECT statement is different.

The way to fix this is to ensure that both SELECT statements return the same number of columns.

Continue reading

Fix “ERROR 1250 (42000): Table ‘…’ from one of the SELECTs cannot be used in global ORDER clause” when using UNION in MySQL

If you’re getting an error that reads “ERROR 1250 (42000): Table ‘…’ from one of the SELECTs cannot be used in global ORDER clause” when using the UNION clause in a MySQL query, it’s probably because you’re qualifying a column name with its table name.

This doesn’t work in MySQL.

To fix this issue, either remove the table name or use a column alias.

Continue reading