Home >> FAQs/Tutorials >> MySQL Tutorials

MySQL FAQs - SQL SELECT Query Statements with GROUP BY

By: FYIcenter.com

Part:   1  2   3  4  5  6 

(Continued from previous part...)

How To Select Some Columns from a Table?

If you want explicitly tell the query to return some columns, you can specify the column names in the SELECT clause. The following select statement returns only two columns, "id" and "url" from the table "fyi_links":

mysql> SELECT id, url FROM fyi_links;
+-----+-------------------+
| id  | url               |
+-----+-------------------+
| 101 | dev.fyicenter.com |
| 102 | dba.fyicenter.com |
| 103 | sqa.fyicenter.com |
+-----+-------------------+
3 rows in set (0.00 sec)

How To Select Some Rows from a Table?

If you don't want select all rows from a table, you can specify a WHERE clause to tell the query to return only the rows that meets the condition defined in the WHERE clause. The WHERE clause condition is a normal Boolean expression. If any data from table needs to be used in the Boolean expression, column names should be used to represent the table data.

The first select statement below only returns rows that have url names containing the letter "a". The second select statement returns no rows, because the WHERE clause results FALSE for all rows in the table.

mysql> SELECT * FROM fyi_links WHERE url LIKE '%a%';
+-----+-------------------+-------+--------+---------------
| id  | url               | notes | counts | created       
+-----+-------------------+-------+--------+---------------
| 102 | dba.fyicenter.com | NULL  |      0 | 2006-07-01 12:
| 103 | sqa.fyicenter.com | NULL  |   NULL | 2006-07-01 12:
+-----+-------------------+-------+--------+---------------
2 rows in set (0.00 sec)

mysql> SELECT * FROM fyi_links WHERE FALSE;
Empty set (0.00 sec)

How To Add More Data to the Testing Table?

If you want to continue with other tutorial exercises in this FAQ collection, you need to add more data to the testing table. Follow the script below to add a new column and :

mysql> ALTER TABLE fyi_links ADD COLUMN tag VARCHAR(8);
mysql> UPDATE fyi_links SET tag = 'DEV' WHERE id = 101;
mysql> UPDATE fyi_links SET tag = 'DBA' WHERE id = 102;
mysql> UPDATE fyi_links SET tag = 'SQA' WHERE id = 103;

mysql> INSERT INTO fyi_links VALUES (104, 
   'www.mysql.com', '', '0', '2006-01-01', 'DBA');
mysql> INSERT INTO fyi_links VALUES (105,
   'www.oracle.com', '', '0', '2005-01-01', 'DBA');
mysql> INSERT INTO fyi_links VALUES (106,
   'www.php.net', '', '0', '2004-01-01', 'DEV');
mysql> INSERT INTO fyi_links VALUES (107, 
   'www.winrunner.com', '', '0', '2003-01-01', 'SQA');

mysql> UPDATE fyi_links SET counts = FLOOR(RAND()*10);

mysql> SELECT id, url, counts, DATE(created), tag 
   FROM fyi_links;
+-----+-------------------+--------+---------------+------+
| id  | url               | counts | DATE(created) | tag  |
+-----+-------------------+--------+---------------+------+
| 101 | dev.fyicenter.com |      4 | 2006-04-30    | DEV  |
| 102 | dba.fyicenter.com |      3 | 2006-07-01    | DBA  |
| 103 | sqa.fyicenter.com |      6 | 2006-07-01    | SQA  |
| 104 | www.mysql.com     |      1 | 2006-01-01    | DBA  |
| 105 | www.oracle.com    |      7 | 2005-01-01    | DBA  |
| 106 | www.php.net       |      4 | 2004-01-01    | DEV  |
| 107 | www.winrunner.com |      8 | 2003-01-01    | SQA  |
+-----+-------------------+--------+---------------+------+
7 rows in set (0.00 sec)

(Continued on next part...)

Part:   1  2   3  4  5  6 

MySQL Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...