MySQL SELECT
How to use the mySQL SELECT command to select data from databases and tables.
The SELECT query is the most commonly-used MySQL query. It is used to select information from a database. The basic syntax of a select query is below:
SELECT columns FROM tablename
To select all columns you type:
SELECT * FROM tablename
If you are using data from all the columns then you can use the asterisk but if you are not using data fom all the columns then there are a few reasons you should definitely select each column name individually because it increases performance (because you are not selecting unneeded data). Second, you can format the output whatever way you want. When it comes to selecting dates then you'll almost always want to format them. The third reason, which may or may not matter, is that the order that the columns are listed in the SELECT query is the order that they are shown. This may not matter most of the time because you can format the way the columns are outputted when you write your PHP ECHO commands.
The following examples will use the following database.
SELECT query - Select all columns
MySQL Code
|
|
$query="SELECT * FROM users";
| |
|
Results
|
|
| John |
Smith |
17 |
Ohio |
| John |
Kennedy |
35 |
Massachusetts |
| Martin |
Gross |
16 |
Florida |
| Brian |
Grossman |
31 |
Maine |
| Eric |
Friedman |
27 |
Texas |
|
|
|
SELECT query - Select certain columns
MySQL Code
|
|
$query="SELECT firstname, lastname, state FROM users";
| |
|
Results
|
|
| John |
Smith |
Ohio |
| John |
Kennedy |
Massachusetts |
| Martin |
Gross |
Florida |
| Brian |
Grossman |
Maine |
| Eric |
Friedman |
Texas |
|
|
|