Difference between revisions of "MySQL"

Jump to navigation Jump to search
436 bytes added ,  11:24, 22 February 2011
→‎SELECT Rows: Added DISTINCT and GROUP BY
(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>


Navigation menu