MySQL select from select: select statement

MySQL, like any other relational database management system, has a SELECT statement in its structure. This is not surprising, because databases store information in themselves primarily in order to retrieve it when there is a need for it. The MySQL select from SELECT statement allows you to do this in many different ways, providing a large set of tools.

MySQL installation

MySQL is implemented both under Windows and under Linux. To install MySQL Ubuntu, it is enough to run two commands in the terminal of this OS:

- Sudo apt-get install mysql-server.

- Sudo apt-get install mysql-client.

For MySQL Windows, just download the appropriate installation packages from the official DBMS website and run them.

General structure of the SELECT statement

The full structure in the MySQL select from SELECT statement is as follows:

1. SELECT - names of columns (fields).

2. FROM - table names.

3. WHERE - the condition by which the selection is made.

Please note that although SQL syntax is not case sensitive, it’s good practice to write operators and reserved keywords (FROM, WHERE, etc.) in capital letters, and the names of tables and their columns in small letters. The simplest example of using the MySQL select from SELECT statement is shown in the figure below.

mysql select from select

The asterisk after the word SELECT is an analog of the keyword ALL and means that all columns must be selected from the given table. If you are only interested in certain fields of the table, they should be indicated in the appropriate place, separated by commas. The same applies to table names - if there are several, specify them all with a comma.

Using WHERE

Obviously, you could not help but notice that the WHERE keyword is missing in the previous example - this is because in this case we do not need it. This operator is optional, and there are many such optional operators in SELECT, but about them a little later. If you add a condition after the table name WHERE and some condition, for example type = 'dog', then in the resulting selection you will get only one row with a pet named Edison. The figure below shows an example. Do not pay attention to the ORDER BY statement yet; here it does not play any role, and we will talk about it a bit later.

mysql windows

It is worth noting that after WHERE, you can also write several conditions, but not with a comma. For this purpose you should use keywords such as AND or OR. It all depends on how your conditions are met. If you are interested in a result that satisfies all the conditions you specified, then there should be AND between them, if the result must satisfy at least one of them, then use OR.

ORDER BY operator

We mentioned that in addition to WHERE, the SELECT statement has many other keywords that can be used to manipulate the resulting selection as required in a particular situation. One such β€œkey” is ORDER BY. It allows you to sort the results of a sample by a specific field, both ascending and descending. To do this, just enter after it the name of the column by the values ​​of which you need to sort the data. It is important to observe several conditions: first, the name specified in the ORDER BY clause must be present in the SELECT itself; the second - ORDER BY must be placed at the very end of the request.

mysql ubuntu

By default, sorting is performed in ascending order (ASC), but if you need to sort the result in descending order, write the DESC keyword after the field name instead of ASC.

Conclusion

We examined the basic, but not all, tools of the MySQL select from operator. SELECT, in addition to the above, has many other features that allow you to flexibly and efficiently retrieve data from the database.

Source: https://habr.com/ru/post/A4571/


All Articles