Learning how to use the AND and OR operators

Some additional expression operators that you can use in MySQL include AND and OR. These are considered logical operators.

You can add additional WHERE clauses as needed using the AND clause. If you want to see all records where g_all is greater than 40 and g_all didn't equal g_batting, you can execute the following query: 

USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all > 40
AND g_all <> g_batting;

You can also add additional WHERE clauses as needed using the OR clause. If you want to see all records where g_all is greater than 40 OR g_defense is greater than 30, you can execute the following query: 

USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE g_all > 40
OR g_defense > 30;

Using parenthesis may become important when filtering results with the WHERE clause. You may only want to see something where both things are true, and something else is false. For example, in the following query, you will get results that are either g_all greater than 60 or the combination of g_all greater than 40 and g_batting less than 30:

USE lahmansbaseballdb;
SELECT playerid, g_all, g_batting, g_defense FROM appearances
WHERE (g_all > 40 AND g_defense < 30)
OR g_all > 60;

The previous query may give you unexpected results since that combination of data may or may not be useful to what you are trying to query. Still, it gives you an example of how to use parenthesis. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.27.45