Difference between revisions of "MySQL"

Jump to navigation Jump to search
427 bytes added ,  16:43, 21 June 2012
→‎SELECT Rows: Updated ORDER BY and JOIN
(→‎Create Users: Renamed to "Create Users and Grant/Revoke Privileges" and added Revoke example)
(→‎SELECT Rows: Updated ORDER BY and JOIN)
Line 170: Line 170:


== SELECT Rows ==
== SELECT Rows ==
'''AS'''
=== AS ===
'''<code>AS</code>''' renames the column heading for a query result.
<source lang="mysql"> SELECT INET_NTOA(ip) AS ip, name FROM hosts; </source>
<source lang="mysql"> SELECT INET_NTOA(ip) AS ip, name FROM hosts; </source>


'''ORDER BY'''
=== ORDER BY ===
<source lang="mysql"> SELECT INET_NTOA(ip) AS ip, name FROM hosts ORDER BY ip; </source>
<source lang="mysql">
SELECT INET_NTOA(ip) AS ip, name FROM hosts ORDER BY ip;
SELECT INET_NTOA(ip) AS ip, name FROM hosts ORDER BY ip DESC;           # Reverse sorting order (descending)
</source>


NULL's can be a bit of pain as they tend to end up at the top, to force then to the bottom insert an additional <code>ISNULL(column)</code> for column your sorting by which has NULL values...
NULL's can be a bit of pain as they tend to end up at the top, to force then to the bottom insert an additional <code>ISNULL(column)</code> for column your sorting by which has NULL values...
Line 186: Line 190:
</source>
</source>


Different types of join will yield differing results, depending how different rows match up.  It can be a bit flummoxing to start with, but its actually fairly simple once you've got the basic idea straight in your head, see - http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html
Different types of join will yield differing results, depending how different rows match up.  It can be a bit flummoxing to start with, but its actually fairly simple once you've got the basic idea straight in your head.  The LEFT and RIGHT joins allow you control whether or not you want to see NULL's appearing in your results where rows don't always have counterparts in two tables you are JOIN'ing together.
 
See this site for a very clear and concise walk-through - http://www.wellho.net/mouth/158_MySQL-LEFT-JOIN-and-RIGHT-JOIN-INNER-JOIN-and-OUTER-JOIN.html


=== COUNT ===
=== COUNT ===

Navigation menu