MorkaLork Development

Interesting stuff I've picked up over the years...

Mysql 2: SELECT

2009-04-16 18:06:37 | 251 views | select column table comand commands from row data matching statement

When you want to extract data from a database you use the command SELECT 'column' FROM. This command get's you a row of data matching the 'column' statement. The statement represents either one column, several or all.


Syntax:

SELECT column
FROM table_name;

SELECT column, column2
FROM table_name;

SELECT *
FROM table_name;


Example:

Results:

If we have this table as a base, we'll see what we get with the differant commands:

|--------------------+--------------------+--------------------|
|Name |Age |Gender |
|____________________+____________________+____________________|
|Bob 34 Male |
|Silvia 67 Female |
|Magnus 24 Male |
|Lisa 32 Female |
+-------------------------------------------------------------+


To select only the name column...


SELECT Name 

FROM person;


|--------------------
|Name |
|____________________
|Bob |
|Silvia |
|Magnus |
|Lisa |
+---------------------+


To select only the name and age column...


SELECT Name, Age 

FROM person;


|--------------------+--------------------|
|Name |Age |
|____________________+____________________|
|Bob 34 |
|Silvia 67 |
|Magnus 24 |
|Lisa 32 |
+-----------------------------------------+


To select everything in the table...


SELECT * 

FROM person;


|--------------------+--------------------+--------------------|
|Name |Age |Gender |
|____________________+____________________+____________________|
|Bob 34 Male |
|Silvia 67 Female |
|Magnus 24 Male |
|Lisa 32 Female |
+-------------------------------------------------------------+





Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.