A little more on the MySQL SELECT statement
The MySQL SELECT command is something like a print or write command of other languages. You can ask it to display text strings, numeric data, the results of mathematical expressions etc.
select version(); +-----------+ | version() | +-----------+ | 3.22.32 | +-----------+ 1 row in set (0.00 sec)
select now(); +---------------------+ | now() | +---------------------+ | 2001-05-31 00:36:24 | +---------------------+ 1 row in set (0.00 sec)
SELECT DAYOFMONTH(CURRENT_DATE); +--------------------------+ | DAYOFMONTH(CURRENT_DATE) | +--------------------------+ | 28 | +--------------------------+ 1 row in set (0.01 sec) SELECT MONTH(CURRENT_DATE); +---------------------+ | MONTH(CURRENT_DATE) | +---------------------+ | 1 | +---------------------+ 1 row in set (0.00 sec) SELECT YEAR(CURRENT_DATE); +--------------------+ | YEAR(CURRENT_DATE) | +--------------------+ | 2001 | +--------------------+ 1 row in set (0.00 sec)
select 'I Love MySQL'; +--------------+ | I Love MySQL | +--------------+ | I Love MySQL | +--------------+ 1 row in set (0.00 sec)
Obviously you can provide pseudo names for these columns using AS.
select 'Manish Sharma' as Name; +---------------+ | Name | +---------------+ | Manish Sharma | +---------------+ 1 row in set (0.00 sec)
select ((4 * 4) / 10 ) + 25; +----------------------+ | ((4 * 4) / 10 ) + 25 | +----------------------+ | 26.60 | +----------------------+ 1 row in set (0.00 sec)
With SELECT you can concatenate values for display. CONCAT accepts arguments between parenthesis. These can be column names or plain text strings. Text strings have to be surrounded with quotes (single or double).
SELECT CONCAT(f_name, " ", l_name) from employee_data where title = 'Programmer'; +-----------------------------+ | CONCAT(f_name, " ", l_name) | +-----------------------------+ | Fred Kruger | | John MacFarland | | Edward Sakamuro | | Alok Nanda | +-----------------------------+ 4 rows in set (0.00 sec)
You can also give descriptive names to these columns using AS.
select CONCAT(f_name, " ", l_name) AS Name from employee_data where title = 'Marketing Executive'; +---------------+ | Name | +---------------+ | Monica Sehgal | | Hal Simlai | | Joseph Irvine | +---------------+ 3 rows in set (0.00 sec)
|
« Previous
|
Next »
|
Possible Answers create database addressbook; OR CREATE DATABASE addressbook; Note: SQL statements are case-insensitive, though table names and database names might be sensitive to case ...
Pattern Matching with text data We will now learn at how to match text patterns using the where clause and the LIKE operator in this ...
selecting data using conditions In this section of the MySQL tutorial we'll look at the format of a SELECT statement we met in the last ...
Logical Operators In this section of the SQL primer we look at how to select data based on certain conditions presented through MySQL logical operators. ...
Querying MySQL tables Our employee_data table now contains enough data for us to work with. Let us see how we can extract (query) it. Querying ...