MySQL supports several types of operators, which can be used to perform various operations on values in your queries.
Here are some of the commonly used operators in MySQL:
These are used for performing arithmetic operations in your queries. The common arithmetic operators in MySQL are
/ (division), and
SELECT column1 + column2 FROM table_name;
This query will return the sum of
column2 for each row in
These are used to compare two values in your queries. The common comparison operators in MySQL are
= (equal to),
!= (not equal to),
> (greater than),
< (less than),
>= (greater than or equal to), and
<= (less than or equal to).
SELECT * FROM table_name WHERE column1 > 100;
These are used to combine multiple conditions in your queries. The common logical operators in MySQL are
SELECT * FROM table_name WHERE column1 > 100 AND column2 < 200;
This query will return all rows in
table_name where the value of
column1 is greater than
100 and the value of
column2 is less than
These are used to assign values to variables or columns in your queries. The common assignment operators in MySQL are
= (simple assignment),
-= (decrement), and
*= (multiply and assign).
SET @var1 = 100; SET @var2 = @var1 + 50; UPDATE table_name SET column1 = column1 + 10 WHERE column2 = 'value';
In this example, the first two statements assign values to variables
@var2, while the third statement updates the value of
column1 by adding
10 to it where the value of
These are some of the commonly used operators in MySQL. Knowing how to use them effectively will allow you to write more complex and powerful queries.