MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL - Equal to (=) Operator



The MySQL = (equal to) operator checks if the value of left operand is equal to the value of right operand and returns true if the condition is true, false otherwise.

Note: The = operator is also used to assign value (for example assigning values with UPDATE statement).

The example below describes how to use this operator in various conditions:

Example:

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

  • Using with WHERE Clause: To select records of table where Age of the person is equal to 28, the query is given below.

    SELECT * FROM Employee
    WHERE Age = 28;
    

    The query will produce following result:

    EmpIDNameCityAgeSalary
    5RameshNew Delhi283000
    6HuangBeijing282800
  • Using with AS Clause: If the above condition is used with AS clause, it will produce a column having boolean result, 1 if the condition is true and 0 if the condition is false. See the example below:

    SELECT *, (Age = 28) AS Result FROM Employee;
    

    The above query will produce following result:

    EmpIDNameCityAgeSalaryResult
    1JohnLondon2530000
    2MarryNew York2427500
    3JoParis2728000
    4KimAmsterdam3031000
    5RameshNew Delhi2830001
    6HuangBeijing2828001
  • Using with UPDATE Clause: The operator can also be used to update a column with boolean result, 1 if the condition is true and 0 if the condition is false. See the example query below:

    UPDATE Employee
    SET Age = (Age = 28);
    
    --See result
    SELECT * FROM Employee;
    

    The query will produce following result:

    EmpIDNameCityAgeSalary
    1JohnLondon03000
    2MarryNew York02750
    3JoParis02800
    4KimAmsterdam03100
    5RameshNew Delhi13000
    6HuangBeijing12800
  • Using with values: A condition can be checked simply by using SELECT statement:

    SELECT 50 = 25;
    

    The query will produce following result:

    50 = 25
    0

❮ MySQL Operators

5