-
-
Save zero-g/b4fd872ca230e1b53ea5 to your computer and use it in GitHub Desktop.
Revisions
-
hofmannsven revised this gist
Sep 18, 2015 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -97,6 +97,18 @@ Get average value: `SELECT AVG([column]) FROM [table];` Get rounded average value and group by `[category-column]`: `SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];` Multiple tables ----------- Select from multiple tables: `SELECT [table1].[column], [table1].[another-column], [table2].[column] FROM [table1], [table2];` Combine rows from different tables: `SELECT * FROM [table1] INNER JOIN [table2] ON [table1].[column] = [table2].[column];` Combine rows from different tables but do not require the join condition: `SELECT * FROM [table1] LEFT OUTER JOIN [table2] ON [table1].[column] = [table2].[column];` (The left table is the first table that appears in the statement.) Rename column or table using an _alias_: `SELECT [table1].[column] AS '[value]', [table2].[column] AS '[value]' FROM [table1], [table2];` Users functions ----------- -
hofmannsven revised this gist
Sep 18, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -97,7 +97,7 @@ Get average value: `SELECT AVG([column]) FROM [table];` Get rounded average value and group by `[category-column]`: `SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];` Users functions ----------- List all users: `select User,Host from mysql.user;` -
hofmannsven revised this gist
Sep 18, 2015 . 3 changed files with 29 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -76,4 +76,32 @@ Export a database dump (more info [here](http://stackoverflow.com/a/21091197/181 Import a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysql -u [username] -p -h localhost [database] < db_backup.sql` Logout: `exit;` Aggregate functions ----------- Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00` Calculate total number of records: `SELECT SUM([column]) FROM [table];` Count total number of `[column]` and group by `[category-column]`: `SELECT [category-column], SUM([column]) FROM [table] GROUP BY [category-column];` Get largest value in `[column]`: `SELECT MAX([column]) FROM [table];` Get smallest value: `SELECT MIN([column]) FROM [table];` Get average value: `SELECT AVG([column]) FROM [table];` Get rounded average value and group by `[category-column]`: `SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];` User functions ----------- List all users: `select User,Host from mysql.user;` Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';` Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';` This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +0,0 @@ This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,8 +0,0 @@ -
hofmannsven revised this gist
Sep 18, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -55,6 +55,8 @@ Select records starting with `val` and ending with `ue`: `SELECT * FROM [table] Select a range: `SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];` Select with custom order and only limit: `SELECT * FROM [table] WHERE [column] ORDER BY [column] ASC LIMIT [value];` (Order: `DESC`, `ASC`) Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];` Deleting records: `DELETE FROM [table] WHERE [column] = [value];` -
hofmannsven revised this gist
Sep 18, 2015 . 1 changed file with 6 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -45,13 +45,15 @@ Counting records: `SELECT COUNT([column]) FROM [table];` Counting and selecting grouped records: `SELECT *, (SELECT COUNT([column]) FROM [table]) AS count FROM [table] GROUP BY [column];` Selecting specific records: `SELECT * FROM [table] WHERE [column] = [value];` (Selectors: `<`, `>`, `!=`; combine multiple selectors with `AND`, `OR`) Select records containing `[value]`: `SELECT * FROM [table] WHERE [column] LIKE '%[value]%';` Select records starting with `[value]`: `SELECT * FROM [table] WHERE [column] LIKE '[value]%';` Select records starting with `val` and ending with `ue`: `SELECT * FROM [table] WHERE [column] LIKE '[val_ue]';` Select a range: `SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];` Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];` -
hofmannsven revised this gist
Sep 18, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -51,6 +51,8 @@ Searching records for a word: `SELECT * FROM [table] WHERE [column] LIKE '%[valu Searching records for a word starting with [value]: `SELECT * FROM [table] WHERE [column] LIKE '[value]%';` Searching for words starting with `val` and ending with `ue`: `SELECT * FROM [table] WHERE [column] LIKE '[val_ue]';` Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];` Deleting records: `DELETE FROM [table] WHERE [column] = [value];` -
hofmannsven revised this gist
Sep 15, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ Commands ----------- List all users: `select User,Host from mysql.user;` Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';` Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';` -
hofmannsven revised this gist
Sep 13, 2015 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,9 @@ MySQL =============== Getting started: - http://www.sqlteaching.com/ - https://www.codecademy.com/courses/learn-sql Related tutorial: http://cd64.de/mysql-cli -
hofmannsven revised this gist
Sep 13, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -66,4 +66,6 @@ Custom column output names: `SELECT [column] AS [custom-column] FROM [table];` Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql` Import a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysql -u [username] -p -h localhost [database] < db_backup.sql` Logout: `exit;` -
hofmannsven revised this gist
Aug 11, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,4 +3,4 @@ Commands Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';` Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';` -
hofmannsven revised this gist
Jul 3, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ Commands ----------- Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00` -
hofmannsven renamed this gist
Jun 14, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
hofmannsven renamed this gist
Jun 14, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
hofmannsven revised this gist
Jun 14, 2015 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ Commands ----------- Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1` -
hofmannsven revised this gist
May 28, 2015 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ Commands ----------- Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';` Grunt `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';` -
hofmannsven revised this gist
Feb 19, 2015 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ MySQL =============== Getting started: http://www.sqlteaching.com/ Related tutorial: http://cd64.de/mysql-cli SQL joins infografic: http://cd64.de/sql-joins -
hofmannsven revised this gist
Nov 8, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -52,6 +52,7 @@ Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column Deleting records: `DELETE FROM [table] WHERE [column] = [value];` Delete *all records* from a table (without dropping the table itself): `DELETE FROM [table];` (This also resets the incrementing counter for auto generated columns like an id column.) Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];` -
hofmannsven revised this gist
Nov 8, 2014 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -51,6 +51,8 @@ Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column Deleting records: `DELETE FROM [table] WHERE [column] = [value];` Delete *all records* from a table (without dropping the table itself): `DELETE FROM [table];` Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];` Deleting tables: `DROP TABLE [table];` @@ -61,4 +63,4 @@ Custom column output names: `SELECT [column] AS [custom-column] FROM [table];` Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql` Logout: `exit;` -
hofmannsven revised this gist
Sep 11, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ [mysqld] max_allowed_packet=64M -
hofmannsven revised this gist
Aug 3, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -59,6 +59,6 @@ Deleting databases: `DROP DATABASE [database];` Custom column output names: `SELECT [column] AS [custom-column] FROM [table];` Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql` Logout: `exit` -
hofmannsven revised this gist
Aug 3, 2014 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ SQL joins infografic: http://cd64.de/sql-joins Commands ----------- Access monitor: `mysql -u [username] -p;` (will prompt for password) Show all databases: `show databases;` @@ -59,4 +59,6 @@ Deleting databases: `DROP DATABASE [database];` Custom column output names: `SELECT [column] AS [custom-column] FROM [table];` Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql` (will prompt for password) Logout: `exit` -
hofmannsven revised this gist
Mar 4, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,8 @@ MySQL Related tutorial: http://cd64.de/mysql-cli SQL joins infografic: http://cd64.de/sql-joins Commands ----------- -
hofmannsven revised this gist
Feb 23, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ MySQL =============== Related tutorial: http://cd64.de/mysql-cli -
hofmannsven revised this gist
Feb 23, 2014 . 1 changed file with 27 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,38 +11,50 @@ Access monitor: `mysql -u [username] -p` (will prompt for password) Show all databases: `show databases;` Access database: `mysql -u [username] -p [database]` (will prompt for password) Create new database: `create database [database];` Select database: `use [database];` Show all tables: `show tables;` Show table structure: `describe [table];` Create new table with columns: `CREATE TABLE [table] ([column] VARCHAR(120), [another-column] DATETIME);` Adding a column: `ALTER TABLE [table] ADD COLUMN [column] VARCHAR(120);` Adding a column with an unique, auto-incrementing ID: `ALTER TABLE [table] ADD COLUMN [column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;` Inserting a record: `INSERT INTO [table] ([column], [column]) VALUES ('[value]', [value]');` MySQL function for datetime input: `NOW()` Selecting records: `SELECT * FROM [table];` Selecting parts of records: `SELECT [column], [another-column] FROM [table];` Counting records: `SELECT COUNT([column]) FROM [table];` Counting and selecting grouped records: `SELECT *, (SELECT COUNT([column]) FROM [table]) AS count FROM [table] GROUP BY [column];` Selecting specific records: `SELECT * FROM [table] WHERE [column] = [value];` (Selectors: `<`, `>`, `!=`) Searching records for a word: `SELECT * FROM [table] WHERE [column] LIKE '%[value]%';` Searching records for a word starting with [value]: `SELECT * FROM [table] WHERE [column] LIKE '[value]%';` Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];` Deleting records: `DELETE FROM [table] WHERE [column] = [value];` Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];` Deleting tables: `DROP TABLE [table];` Deleting databases: `DROP DATABASE [database];` Custom column output names: `SELECT [column] AS [custom-column] FROM [table];` Logout: `exit` -
hofmannsven revised this gist
Feb 23, 2014 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -35,4 +35,14 @@ Selecting records: `SELECT * FROM [name-of-table];` Selecting parts of records: `SELECT [name-of-column], [name-of-another-column] FROM [name-of-table];` Counting records: `SELECT COUNT([name-of-column]) FROM [name-of-table];` Counting and selecting grouped records: `SELECT *, (SELECT COUNT([name-of-column]) FROM [name-of-table]) AS count FROM [name-of-table] GROUP BY [name-of-column];` Selecting specific records: `SELECT * FROM [name-of-table] WHERE [name-of-column] = [custom-input];` (Selectors: `<`, `>`, `!=`) Searching records for a word: `SELECT * FROM [name-of-table] WHERE [name-of-column] LIKE '%[custom-input]%';` Searching records for a word starting with [custom-input]: `SELECT * FROM [name-of-table] WHERE [name-of-column] LIKE '[custom-input]%';` Logout: `exit` -
hofmannsven revised this gist
Feb 23, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ Adding a column: `ALTER TABLE [name-of-table] ADD COLUMN [name-of-column] VARCHA Adding a column with an unique, auto-incrementing ID: `ALTER TABLE [name-of-table] ADD COLUMN [name-of-column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;` Inserting a record: `INSERT INTO [name-of-table] ([name-of-column], [name-of-column]) VALUES ('[custom-input]', [custom-input]');` MySQL function for datetime input: `NOW()` -
hofmannsven revised this gist
Feb 23, 2014 . 1 changed file with 13 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -21,6 +21,18 @@ Show all tables: `show tables;` Show table structure: `describe [name-of-table];` Create new table with columns: `CREATE TABLE [name-of-table] ([name-of-column] VARCHAR(120), [name-of-another-column] DATETIME);` Adding a column: `ALTER TABLE [name-of-table] ADD COLUMN [name-of-column] VARCHAR(120);` Adding a column with an unique, auto-incrementing ID: `ALTER TABLE [name-of-table] ADD COLUMN [name-of-column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;` Inserting a record: `INSERT INTO [name-of-table] ([name-of-column], [name-of-another-column]) VALUES ('[custom-input]', [custom-input]');` MySQL function for datetime input: `NOW()` Selecting records: `SELECT * FROM [name-of-table];` Selecting parts of records: `SELECT [name-of-column], [name-of-another-column] FROM [name-of-table];` Logout: `exit` -
hofmannsven created this gist
Feb 23, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ MySQL Command Line Interface =============== Related tutorial: http://cd64.de/mysql-cli Commands ----------- Access monitor: `mysql -u [username] -p` (will prompt for password) Show all databases: `show databases;` Access database: `mysql -u [username] -p [name-of-database]` (will prompt for password) Create new database: `create database [name-of-database];` Select database: `use [name-of-database];` Show all tables: `show tables;` Show table structure: `describe [name-of-table];` Create tables and columns: `CREATE TABLE [name-of-table] ([name-of-column] VARCHAR(120), [name-of-another-column] DATETIME);` Logout: `exit` This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ alias mysql=/Applications/MAMP/Library/bin/mysql