An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ select ’

Sql Select Statement

by Victor | September 2, 2008 in Sql | No Comments

General Select Format

SELECT commaSeperatedColumnNames FROM tableName

Below, we will go over the basics of selecting from a table in various situations. Below is a standards table with various columns and various rows. This table will be used through the rest of this article.

Table: members

id name age gender state
1 Victor 18 M CA
2 Bill 28 M NY
3 Jill 5 F CA
4 Bob 14 M AL

Select All

SELECT * FROM members

Select Males

SELECT * FROM members WHERE gender = ‘M’

Select Age Between 10 and 20

SELECT * FROM members WHERE age between ‘10′ AND ‘20′

Select Distinct States

SELECT distinct(state) FROM members

Select All Names, But Rename Column To FirstName

SELECT name as FirstName FROM members

To randomly select a row from a MySql table, use the following:

SELECT * FROM tableName ORDER BY RAND() LIMIT 1

The RAND() function will randomly sort the selected data. The LIMIT X will select the first X data rows. In this example, X equals 1, so it is the equivalent to one row randomly selected. If you want to randomly select more than one row, set X to the number of random rows you want to retrieve.