The previous lesson of MySql Lessons series was about MySql Insert Query and in this
lesson we will learn about select query in MySql.
MySql Select Query is used to select a block of data from a table according to the specifications and conditions provided in the query. For the explanation and examples of select query consider the following table:
The above table contains a set of records of users having 3 fields i.e id, Name and Email. Now let us write our first select query to fetch all the records in the table as it is i.e
If we execute the above query in the SQL panel, it will fetch and show all records in the table "testtable". Here the * in the query means to fetch all the fields of the table. If we want to fetch only id and Name of the users and not their Emails. Then this is how we can do it:
MySql Where Clause is very important for the select, update and delete operations in MySql. For now we will look at it's use in the select operation. Where Clause is used in a query whenever we want to apply conditions to our query operation. For example: In the above table if we want to show the record of user whose name is Albert we will have to apply where clause to our query like this:
The above query will show the record of the user whose Name is Albert. Similarly we can apply the condition on any of the 3 fields to show only the block of data we need.
AND and OR are used in a query to add extra conditions to the where clause. For example: If we want to fetch data of users whose names are Albert and Tom we will have to use an OR condition in the where clause to apply 2 conditions to the query i.e
Similarly if we want to fetch the record of a user whose name is Albert and his id is 4, we will have to apply AND condition to the query here i.e
The above query will return null result because there is no such user in the table whose name is Albert and his id is 4. There is 1 such user in the table whose name is Albert but his id is 3, so in AND condition both requirements should be met for a record to be fetched.
MySql Select Query is used to select a block of data from a table according to the specifications and conditions provided in the query. For the explanation and examples of select query consider the following table:
The above table contains a set of records of users having 3 fields i.e id, Name and Email. Now let us write our first select query to fetch all the records in the table as it is i.e
SELECT * FROM testtable
If we execute the above query in the SQL panel, it will fetch and show all records in the table "testtable". Here the * in the query means to fetch all the fields of the table. If we want to fetch only id and Name of the users and not their Emails. Then this is how we can do it:
SELECT id, Name FROM testtable
MySql Where Clause?
MySql Where Clause is very important for the select, update and delete operations in MySql. For now we will look at it's use in the select operation. Where Clause is used in a query whenever we want to apply conditions to our query operation. For example: In the above table if we want to show the record of user whose name is Albert we will have to apply where clause to our query like this:
SELECT * FROM testtable WHERE Name = 'Albert'
The above query will show the record of the user whose Name is Albert. Similarly we can apply the condition on any of the 3 fields to show only the block of data we need.
AND and OR Conditions in Mysql
AND and OR are used in a query to add extra conditions to the where clause. For example: If we want to fetch data of users whose names are Albert and Tom we will have to use an OR condition in the where clause to apply 2 conditions to the query i.e
SELECT * FROM testtable WHERE Name = 'Albert' OR Name = 'Tom'
Similarly if we want to fetch the record of a user whose name is Albert and his id is 4, we will have to apply AND condition to the query here i.e
SELECT * FROM testtable WHERE Name = 'Albert' AND id = 4
The above query will return null result because there is no such user in the table whose name is Albert and his id is 4. There is 1 such user in the table whose name is Albert but his id is 3, so in AND condition both requirements should be met for a record to be fetched.
No comments:
Post a Comment