MySQL AND operator
The One-off Mysql Logical operator is AND . The purpose of this operator is to combine two or more Boolean expressions and returns true only if both expressions evaluate to be true.If one-off condition becomes false then AND opertor returns false
To filter the result in the WHERE clause of the SELECT, UPDATE, DELETE the AND operator is used.The AND operator is also used with join or left join clause.
The AND operator in MySQL is used to combine multiple conditions in a query and return only the rows that meet all the specified conditions. The syntax for using the AND operator in a MySQL query is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND ... AND conditionN;
For example, to select all rows from a table “customers” where the “country” is “USA” and the “city” is “New York”, you would use the following query:
SELECT *
FROM customers
WHERE country = 'USA' AND city = 'New York';
Table structure Used for Example explanation
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| id | int(11) unsigned | NO | PRI | NULL | |
| name | varchar(255) | NO | UNI | NULL | |
| code | varchar(3) | NO | UNI | NULL | |
| population | int(11) | NO | | NULL | |
| currency_code | varchar(3) | YES | | NULL | |
| capital | varchar(255) | YES | | NULL | |
| area | double | YES | | NULL | |
| time_zone | varchar(255) | YES | | NULL | |
| languages | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------------+------------------+------+-----+---------+-------+
This table has the following fields:
id
: a unique identifier for each country, using an unsigned integer.name
: the full name of the country, using a string of up to 255 characters.code
: the ISO 3166-1 alpha-3 code for the country, using a string of 3 characters.population
: the population of the country, using an integer.currency_code
: the ISO 4217 currency code for the country, using a string of 3 characters.capital
: the name of the capital city of the country, using a string of up to 255 characters.area
: the total land area of the country, using a double precision number.time_zone
: the time zone of the country, using a string of up to 255 characters.languages
: the official languages of the country, using a string of up to 255 characters.created_at
: the timestamp for when the country was added to the table, using a datetime field.updated_at
: the timestamp for when the country was last updated, using a datetime field.
Note: This is just one possible structure for a “Countries” table, and it can be adjusted based on your specific needs and requirements.
Example for MySQL AND operator
SELECT Name, Population FROM Country WHERE Population > 100000000 AND (Continent = 'Asia' );
Output of Above Example
+————+————+
| Name | Population |
+————+————+
| Bangladesh | 129155000 |
| Indonesia | 212107000 |
| India | 1013662000 |
| Japan | 126714000 |
| China | 1277558000 |
| Pakistan | 156483000 |