database:mysql
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| database:mysql [2023/11/02 08:58] – [Mysql basic commands] skipidar | database:mysql [2023/11/02 11:04] (current) – skipidar | ||
|---|---|---|---|
| Line 431: | Line 431: | ||
| HAVING COUNT(o.OrderID) < 20 | HAVING COUNT(o.OrderID) < 20 | ||
| ORDER BY Sales DESC, FirstName DESC, LastName | ORDER BY Sales DESC, FirstName DESC, LastName | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | == Exist == | ||
| + | |||
| + | |||
| + | <sxh sql> | ||
| + | --- suppliers with products of price under 20 | ||
| + | SELECT SupplierName | ||
| + | FROM Suppliers | ||
| + | WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID = Suppliers.supplierID AND Price < 20); | ||
| + | |||
| + | |||
| + | |||
| + | --- employees with orders | ||
| + | SELECT LastName | ||
| + | FROM Employees | ||
| + | WHERE NOT EXISTS (SELECT OrderID FROM Orders WHERE Orders.EmployeeID = Employees.EmployeeID); | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | == ANY, ALL== | ||
| + | |||
| + | The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values. | ||
| + | |||
| + | |||
| + | |||
| + | <sxh sql> | ||
| + | |||
| + | --- product name with productID having quantity=10 somewhere | ||
| + | SELECT ProductName | ||
| + | FROM Products | ||
| + | WHERE ProductID = ANY | ||
| + | (SELECT ProductID | ||
| + | FROM OrderDetails | ||
| + | WHERE Quantity = 10); | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | == ANY, ALL== | ||
| + | |||
| + | The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values. | ||
| + | |||
| + | |||
| + | <sxh sql> | ||
| + | |||
| + | SELECT OrderID, Quantity, | ||
| + | CASE | ||
| + | WHEN Quantity > 30 THEN 'The quantity is greater than 30' | ||
| + | WHEN Quantity = 30 THEN 'The quantity is 30' | ||
| + | ELSE 'The quantity is under 30' | ||
| + | END AS QuantityText | ||
| + | FROM OrderDetails; | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | == CASE WHEN == | ||
| + | |||
| + | Case expression | ||
| + | |||
| + | <sxh sql> | ||
| + | |||
| + | SELECT OrderID, Quantity, | ||
| + | CASE | ||
| + | WHEN Quantity > 30 THEN 'The quantity is greater than 30' | ||
| + | WHEN Quantity = 30 THEN 'The quantity is 30' | ||
| + | ELSE 'The quantity is under 30' | ||
| + | END AS QuantityText | ||
| + | FROM OrderDetails; | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | == IFNULL(), ISNULL(), COALESCE(), and NVL() Functions == | ||
| + | |||
| + | | '' | ||
| + | | '' | ||
| + | | '' | ||
| + | |||
| + | <sxh sql> | ||
| + | |||
| + | --- fall back to 0 for NULL " | ||
| + | SELECT ProductName, | ||
| + | FROM Products; | ||
| + | |||
| + | |||
| + | --- using boolean ISNULL | ||
| + | SELECT OrderID, | ||
| + | CASE | ||
| + | WHEN ISNULL(AmountDue) THEN "Paid in full" | ||
| + | WHEN DATE(DueDate) < date(NOW()) THEN "Order is past due" | ||
| + | ELSE CONCAT(" | ||
| + | END | ||
| + | FROM OrderDetails; | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | == Stored Procedure Example == | ||
| + | |||
| + | |||
| + | <sxh sql> | ||
| + | |||
| + | --- procedure | ||
| + | CREATE PROCEDURE SelectAllCustomers | ||
| + | AS | ||
| + | SELECT * FROM Customers | ||
| + | GO; | ||
| + | |||
| + | --- execute | ||
| + | EXEC SelectAllCustomers; | ||
| + | |||
| + | |||
| + | |||
| + | --- procedure with args | ||
| + | CREATE PROCEDURE SelectAllCustomers @City nvarchar(30) | ||
| + | AS | ||
| + | SELECT * FROM Customers WHERE City = @City | ||
| + | GO; | ||
| + | |||
| + | --- exec | ||
| + | EXEC SelectAllCustomers @City = ' | ||
| </ | </ | ||
database/mysql.1698915531.txt.gz · Last modified: by skipidar
