Created
December 22, 2017 07:23
-
-
Save pohzipohzi/8b8784df3aca63b5991d790e6a591b63 to your computer and use it in GitHub Desktop.
Some lesser known sql functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Replace a table using data and structure from another one: | |
DROP TABLE IF EXISTS a | |
CREATE TABLE a LIKE b | |
INSERT INTO a SELECT * FROM b | |
Insert into table with possibility of duplicate: | |
INSERT INTO a(`f1`,`f2`) VALUES ('x','y') ON DUPLICATE KEY UPDATE `f2`='y' | |
or | |
INSERT IGNORE INTO a(`f1`,`f2`) VALUES ('x','y') | |
Nesting select statements: | |
SELECT SUM(tbl_inner.Total) AS Total | |
FROM (SELECT SUM(t) AS Total FROM tbl) tbl_inner | |
Concatenating tables vertically (union): | |
SELECT * FROM ((SELECT * FROM `Account`) UNION ALL ( SELECT * FROM `Account`)) a | |
Double left join for same table: | |
LEFT JOIN Prices AS TodaysPrices ON Prices.Date=CURDATE() | |
LEFT JOIN Prices AS YesterdaysPrices ON Prices.Date=SUBDATE(CURDATE(), 1) | |
Order by with like: | |
ORDER BY CASE | |
WHEN name LIKE "%John%" THEN 1 | |
WHEN name LIKE "%Doen%" THEN 2 | |
ELSE 3 | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment