MySQL ERROR 1365 occurs when a query divides by zero, halting execution. Fix it using NULLIF(), CASE statements, or filtering with WHERE clauses to handle zero denominators safely.
MySQL Error 1365 (SQLSTATE 22012) is triggered when a SQL expression attempts division by zero, which is mathematically undefined. This error can occur during SELECT, UPDATE, INSERT...SELECT, or stored procedure execution. When MySQL encounters a division by zero, it immediately stops processing the statement and raises this error to prevent invalid calculations. By default, division by zero would return NULL, but depending on your SQL mode settings (particularly ERROR_FOR_DIVISION_BY_ZERO combined with strict mode), MySQL enforces this as an error to ensure data integrity and protect against unexpected NULL values in your results.
The NULLIF() function returns NULL if the divisor equals zero, preventing the division operation:
SELECT
total_sales,
revenue / NULLIF(units_sold, 0) AS price_per_unit
FROM sales
WHERE date >= CURDATE();When units_sold is 0, NULLIF() returns NULL, making the division result NULL instead of an error. This is the most concise solution for most use cases.
Add explicit conditional logic to handle the zero case:
SELECT
total_sales,
CASE
WHEN units_sold = 0 THEN NULL
ELSE revenue / units_sold
END AS price_per_unit
FROM sales;CASE provides more control—you can return NULL, 0, a default value, or even log the condition. Use this when you need custom behavior beyond just returning NULL.
The IF() function provides a concise alternative to CASE for simple conditions:
SELECT
total_sales,
IF(units_sold = 0, NULL, revenue / units_sold) AS price_per_unit
FROM sales;IF() is equivalent to the CASE statement but more compact. It returns NULL when units_sold is 0, otherwise performs the division.
Exclude rows where the divisor is zero before performing calculations:
SELECT
total_sales,
revenue / units_sold AS price_per_unit
FROM sales
WHERE units_sold > 0;This approach works when you only need results for valid calculations. However, it loses rows where the divisor is zero, which may not always be acceptable.
For columns that should never be zero, add a CHECK constraint when defining the table:
CREATE TABLE sales (
id INT PRIMARY KEY,
units_sold INT NOT NULL CHECK (units_sold > 0),
revenue DECIMAL(10, 2) NOT NULL
);Or on an existing table:
ALTER TABLE sales
ADD CONSTRAINT check_units_positive CHECK (units_sold > 0);This enforces data integrity at the database level, preventing problematic data from being inserted in the first place.
Check your current SQL mode to understand when division by zero is treated as an error:
SELECT @@sql_mode;If ERROR_FOR_DIVISION_BY_ZERO is enabled (typically as part of STRICT_ALL_TABLES or TRADITIONAL mode), division by zero will error during INSERT/UPDATE/CREATE TABLE operations. To temporarily disable strict mode:
SET sql_mode='';Or remove just the error flag:
SET sql_mode='STRICT_TRANS_TABLES';Note: Changing sql_mode at runtime only affects the current session. To make permanent changes, edit your MySQL configuration file (my.cnf or my.ini) and restart the server.
Virtual/Generated Columns: MySQL 5.7 and early versions of 8.0 have a known bug where division by zero in generated columns can cause data consistency issues. If you encounter this, the workaround is to drop the generated columns, fix the underlying data to remove zero divisors, and then recreate the columns. SQL Mode Details: The ERROR_FOR_DIVISION_BY_ZERO flag only affects DML operations (INSERT, UPDATE, CREATE TABLE). SELECT statements always return NULL for division by zero, regardless of SQL mode. If you need stricter behavior in application code, validate divisors before executing queries. Performance Note: NULLIF() and CASE are index-friendly and have minimal performance impact. However, if checking many rows, adding a WHERE clause or CHECK constraint prevents the overhead entirely by preventing invalid data from being stored.
ERROR 1064: You have an error in your SQL syntax
How to fix "ERROR 1064: You have an error in your SQL syntax" in MySQL
ERROR 1054: Unknown column in field list
Unknown column in field list
ER_WINDOW_RANGE_FRAME_NUMERIC_TYPE (3589): RANGE frame requires numeric ORDER BY expression
RANGE frame requires numeric ORDER BY expression in MySQL window functions
CR_ALREADY_CONNECTED (2058): Handle already connected
How to fix "CR_ALREADY_CONNECTED (2058): Handle already connected" in MySQL
ER_WINDOW_DUPLICATE_NAME (3591): Duplicate window name
How to fix ER_WINDOW_DUPLICATE_NAME (3591) in MySQL window functions