using the MySQL SELECT statement to only select certain records.

Posted: August 17, 2012. At: 9:16 PM. This was 5 years ago. Post ID: 4444
Page permalink: http://securitronlinux.com/bejiitaswrath/using-the-mysql-select-statement-to-only-select-certain-records/

We are taking another look at our database table and we are wanting to only view records that match certain criteria. This is easy with the SELECT statement.

mysql> select * from operatingsystems where vendor in ("Microsoft","Mint");
+------+------------------------+---------+-----------+
| OsID | Name                   | Type    | vendor    |
+------+------------------------+---------+-----------+
|    1 | Windows 3.0            | Windows | Microsoft |
|    2 | Windows 3.1            | Windows | Microsoft |
|    3 | Windows `95            | Windows | Microsoft |
|    4 | Windows `98            | Windows | Microsoft |
|   21 | Linux Mint 8           | Linux   | Mint      |
|   22 | Linux Mint 9           | Linux   | Mint      |
|   23 | Linux Mint 10          | Linux   | Mint      |
|   24 | Linux Mint 12          | Linux   | Mint      |
|   25 | Linux Mint 13          | Linux   | Mint      |
|   29 | Windows 7              | Windows | Microsoft |
|   30 | Windows Vista Ultimate | Windows | Microsoft |
+------+------------------------+---------+-----------+
11 rows in set (0.00 sec)

Another example.

mysql> select * from operatingsystems where vendor in ('Redhat','Mandrake');
+------+--------------------+-------+----------+
| OsID | Name               | Type  | vendor   |
+------+--------------------+-------+----------+
|    5 | Redhat 6.2         | Linux | Redhat   |
|    7 | Mandrake Linux 9.0 | Linux | Mandrake |
|    8 | Mandrake Linux 9.2 | Linux | Mandrake |
+------+--------------------+-------+----------+
3 rows in set (0.00 sec)

This is how you use the SELECT statement to retrieve records from a MySQL database. This is a key skill in database management and it is important to master this.

No comments have been made. Use this form to start the conversation :)

Leave a Reply