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:
Arithmetic operators
These are used for performing arithmetic operations in your queries. The common arithmetic operators in MySQL are +
(addition), -
(subtraction), *
(multiplication), /
(division), and %
(modulus).
For example:
SELECT column1 + column2 FROM table_name;
This query will return the sum of column1
and column2
for each row in table_name
.
Comparison Operators
These are used to compare two values in your queries. The common comparison operators in MySQL are =
(equal to), <>
or !=
(not equal to), >
(greater than), <
(less than), >=
(greater than or equal to), and <=
(less than or equal to).
For example:
SELECT * FROM table_name WHERE column1 > 100;
Logical Operators
These are used to combine multiple conditions in your queries. The common logical operators in MySQL are AND
, OR
, and NOT
.
For example:
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 200
.
Assignment Operators
These are used to assign values to variables or columns in your queries. The common assignment operators in MySQL are =
(simple assignment), +=
(increment), -=
(decrement), and *=
(multiply and assign).
For example:
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 @var1
and @var2
, while the third statement updates the value of column1
by adding 10
to it where the value of column2
is 'value'
.
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.