Where clause in MySQL



Hello and welcome everyone. In this article, we will learn about the WHERE clause in MySQL. Let’s go.

MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/

MySQL WHERE clause is used to get specific result from the database table with specific condition as well as it filter records. You can use WHERE clause with SELECT statement as well as DELETE and UPDATE statement.

Syntax:
With SELECT statement syntax:
         SELECT *
     FROM table_name
     WHERE condition;
Here is
SELECT – this command retrieve data from the database table.
table_name – this is table name which table data you want to retrieve.
WHERE – this WHERE clause using specific record will retrieve.
condition – specify the condition for the update.

With UPDATE statement syntax:
         UPDATE table_name
         SET column_name = values, …
         WHERE condition;
Here is
UPDATE – this command updates the values or data or records in the table.
table_name – this is table name which table data you want to update.
SET – it specifies which columns record you want to update with the new values. For update, many rows record you can use a comma separated.
column_name – this column name is which column records you want to update.
values – this one is you can set new values for an update on previous values.
WHERE – this WHERE clause using specific record will update.
condition – specify the condition for the update.

With DELETE statement syntax:
         DELETE FROM
         table_name
         WHERE condition
Here is
DELETE FROM – it is a delete rows data command in MySQL
table_name – which tables row data you want to delete then specify the table name here.
WHERE condition – if you want to specific row delete then use WHERE condition. If the condition matches with rows then it will be deleted otherwise MySQL fetch error.

For Example:
Here is the, we will create a table for an employee in the database.

CREATE TABLE IF NOT EXISTS employee (
                   e_id int (10) PRIMARY KEY,
                   e_firstname varchar (50) NOT NULL,
                   e_lastname varchar (50) NOT NULL,
                   e_age int (10),
                   e_city varchar (250)
           e_sex char (1)
           e_phnumber int (15) NOT NULL
)

Here we insert record in the employee table.
INSERT INTO employee (
  ‘e_id’, ’e_firstname’, ’e_lastname’,
  ’e_age’, ’e_city’, ’e_sex’,
  ’e_phnumber’
)
VALUES
  (
    1, ‘Hone’, ‘Reus’, 25, ‘San Jose’, ‘M’, ‘ + 1 555 555 1111’
  ),
  (
    2, ‘Cries’, ’Fence’, 23, ’Fresno’, ’F’, ’ +1 555 555 1111’
  );

Final Result
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’  ’e_sex’ ’e_phnumber’
  1    Hone        Reus     25   San Jose    M   +1 555 555 1111
 2    Cries         Fence      23  Fresno       F    +1 555 555 1111
With SELECT statement example:

SELECT
e_id, e_firstname, e_lastname FROM employee
          WHERE Age > 20;

With UPDATE statement example:
         UPDATE employee
         SET e_age = 26, e_city = ‘Oakland’
         WHERE e_id = 2;
Then it will update record.
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’   ’e_sex’ ’e_phnumber’
  2     Cries        Fence      26    Oakland    F   +1 555 555 1111

With DELETE statement example:
         DELETE FROM employee
         WHERE e_age = ‘23’;
After executing above query then row number 2 will deleted.

In this article, we learned about WHERE clause. You can use WHERE clause with SELECT, DELETE, and UPDATE statement.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/

Web API 2- .Net Core In depth In 5 Hours click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/



Comments

Popular posts from this blog

ALTER statement in MySQL

Order By Clause in MySQL

MySQL Constraint – PRIMARY KEY