MySQL DISTINCT and LIMIT Example
SELECT * FROM customers;
+---------+------------+-----------+
| Cust_id | First_Name | Last_Name |
+---------+------------+-----------+
| 1 | Mark | James |
| 2 | Paul | James |
| 3 | Sam | James |
| 4 | Paul | Dodds |
+---------+------------+-----------+
If we just want the database to return just the last name, we would use
SELECT Last_Name FROM customers;
+-----------+
| Last_Name |
+-----------+
| James |
| James |
| James |
| Dodds |
+---------+
Now we have duplicates, to remove these use
SELECT DISTINCT Last_Name FROM customers;
+-----------+
| Last_Name |
+-----------+
| James |
| Dodds |
+-----------+
If we had a large database and we just want the first three results.
SELECT tile FROM products LIMIT 3;
+---------+
| tile |
+---------+
| Shoes |
| Soap |
| Shampoo |
+---------+
Now we still want 3 results but not the first one..
SELECT tile FROM products LIMIT 1, 3;
+---------+
| tile |
+---------+
| Soap |
| Shampoo |
| DVD |
+---------+
As the first position in the database starts at 0, we need to state the first item we want should start at 1.
No comments:
Post a Comment