/ / MySQL select from select: the sampling operator

MySQL select from select: the sampling operator

MySQL, like any other relational systemdatabase management, has in its structure a SELECT selection operator. This is not surprising, because databases store information in themselves primarily in order to extract it when necessary. Operator MySQL select from SELECT allows you to do this in a variety of ways, by providing a large set of tools.

Installing MySQL

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

- sudo apt-get install mysql-server.

- sudo apt-get install mysql-client.

For MySQL Windows, it is enough to download the corresponding installation packages from the official "DBMS" site and run them.

The general structure of the SELECT statement

The complete structure in MySQL select from SELECT statement looks like this:

1. SELECT - the names of the columns (fields).

2. FROM - table names.

3. WHERE is the condition for sampling.

Note that although the syntaxThe SQL language is not case-sensitive; it's a good idea to write operators and reserved keywords (FROM, WHERE, etc.) in capital letters, and the names of the tables and their columns are small. The simplest example of using an operator MySQL select from SELECT is shown in the figure below.

mysql select from select

An asterisk after the word SELECT is an analogkeyword ALL and means that you must select all columns from the specified table. If you are interested in only some fields of the table, they should be indicated in the appropriate place through a comma. The same applies to the names of the tables - if there are more than one, enter them all separated by commas.

Using the WHERE clause

Obviously, you could not help but notice that inThe previous example does not have the keyword WHERE - this is because in this case we do not need it. This operator is not mandatory, and there are many such optional operators in SELECT, but they are a little later. If you add after the WHERE table name and any condition, for example type = 'dog', then in the resultant selection you get only one row with the pet named Edison. The figure below shows an example. Do not pay attention to the ORDER BY clause, it does not play any role, and it will be discussed a little later.

mysql windows

It should be noted that after WHERE it is also possiblewrite several conditions, but not through a comma. For this purpose, you must use keywords such as AND or OR. It all depends on how your conditions should be respected. If you are interested in a result that satisfies all the conditions you specify, then there must be an AND between them, and if the result should satisfy at least one of them, then use OR.

ORDER BY clause

We mentioned that in addition to WHERE in the SELECT statementThere are many other keywords with which you can 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, either in ascending or descending order. To do this, you just need to specify the name of the column after it, 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 want 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, of the MySQL select from. SELECT, in addition to the above, has a lot of other features that allow you to extract data from the database flexibly and efficiently.