2,187
edits
(Added SHOW) |
(→SELECT Rows: Added DISTINCT and GROUP BY) |
||
Line 166: | Line 166: | ||
SELECT COUNT(*) FROM hware WHERE make='IBM'; # Counts number of rows in selection | SELECT COUNT(*) FROM hware WHERE make='IBM'; # Counts number of rows in selection | ||
SELECT COUNT(*) AS total, SUM(IF(model='x336',1,0)) AS x336 FROM hware WHERE make='IBM'; # Additional counts number of rows where model='x336' | SELECT COUNT(*) AS total, SUM(IF(model='x336',1,0)) AS x336 FROM hware WHERE make='IBM'; # Additional counts number of rows where model='x336' | ||
</source> | |||
=== DISTINCT === | |||
Only selects completely distinct rows (ie on all columns), not to be confused with <code>GROUP BY</code> (see below). | |||
<source lang="mysql"> | |||
SELECT DISTINCT * FROM hware JOIN notes ON (id); | |||
</source> | |||
=== GROUP BY === | |||
Select rows, grouped by a particular column (so effectively only shows one row for duplicates on that column) | |||
<source lang="mysql"> | |||
SELECT * FROM hware JOIN notes ON (id) GROUP BY model; | |||
</source> | </source> | ||