Wednesday, June 15, 2011

SQL commands Insert,Delete,Create,Select

 

Show all data in a table.

mysql> SELECT * FROM [table name];

Returns the columns and column information about a table.

mysql> show columns from [table name];

 

Show certain selected rows with a certain value.

mysql> SELECT * FROM [table name] WHERE [field name] = "value";

 

Show all records containing a name AND a phone number.

mysql> SELECT * FROM [table name] WHERE name = "name" AND phone_number = 'number';

 

Show all records not containing a specified name AND a phone number order by the phone_number field.

mysql> SELECT * FROM [table name] WHERE name != " " AND phone_number = ' ' order by phone_number;

 

Show all records starting with the letters ' a' AND a phone number ''.

mysql> SELECT * FROM [table name] WHERE name like "a%" AND phone_number = '';

 

Show all records starting with the letters 'b' AND the phone number ' ' limit to records 1 through 5.

mysql> SELECT * FROM [table name] WHERE name like "b%" AND phone_number = ' ' limit 1,5;

 

Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with a.

mysql> SELECT * FROM [table name] WHERE rec RLIKE "^a";

 

Show unique records.

mysql> SELECT DISTINCT [column name] FROM [table name];

 

Show selected records sorted in an ascending (asc) or descending (desc).

mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;

 

Return number of rows.

mysql> SELECT COUNT(*) FROM [table name];

 

Sum column.

mysql> SELECT SUM(*) FROM [table name];

No comments:

Post a Comment