-
-
Save logbasex/c729d97f244bf9bab6a882f5860d7b86 to your computer and use it in GitHub Desktop.
Revisions
-
logbasex revised this gist
Nov 3, 2020 . 1 changed file with 2 additions and 2 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 @@ -2,9 +2,9 @@ ## INSTALL **[Access mysql from docker container](https://stackoverflow.com/questions/28389458/execute-mysql-command-from-the-host-to-container-running-mysql-server)** `docker exec -t -i {docker_container_id/name} mysql -uroot -proot` ## TERMS -
logbasex revised this gist
Nov 3, 2020 . 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 @@ -2,7 +2,9 @@ ## INSTALL **Access mysql from docker container** `docker exec -t -i {docker_container_id} mysql -uroot -proot` ## TERMS -
logbasex revised this gist
Nov 3, 2020 . 2 changed files with 4 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,5 +1,9 @@ # MySQL ## INSTALL Access mysql from docker container: docker exec -t -i {docker_container_id} mysql -uroot -proot ## TERMS **[MySQL terminology: processes, threads & connections]**(http://code.openark.org/blog/mysql/mysql-terminology-processes-threads-connections) 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 +0,0 @@ -
logbasex revised this gist
Oct 30, 2020 . 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 @@ -1,5 +1,9 @@ # MySQL ## TERMS **[MySQL terminology: processes, threads & connections]**(http://code.openark.org/blog/mysql/mysql-terminology-processes-threads-connections) ## Tools - [TablePlus](https://tableplus.io/) - [DataGrip](https://www.jetbrains.com/datagrip/) -
logbasex revised this gist
Oct 30, 2020 . 1 changed file with 3 additions and 3 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 @@ -235,17 +235,17 @@ SET FOREIGN_KEY_CHECKS=1; -- to re-enable them ## Check AUTO_INCREMENT value SHOW TABLE STATUS FROM `database_name` WHERE `name` LIKE 'table_name'; #### LAST_INSERT_ID() -- --------------NOT WORK IF INSERT ID------------------ `insert into table_name (id, name) values (1, value);` `select LAST_INSERT_ID();` -- --------------RUN TWO QUERIES AT ONCE----------------- `insert into table_name (name) values (value);` `select LAST_INSERT_ID();` -
logbasex revised this gist
Oct 30, 2020 . 1 changed file with 49 additions and 18 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 @@ -148,45 +148,73 @@ Use `--lock-tables=false` option for locked tables (more info [here](http://stac `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];` ## 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 **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';` ## Find out the IP Address of the Mysql Host `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646)) ## MySQL function Like concat: `SELECT url FROM media where url like concat('%','youtube','%');` @@ -202,6 +230,9 @@ SET FOREIGN_KEY_CHECKS=1; -- to re-enable them # Problems ## Format date `SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM table_reference;` (Default date format of MySQL is: `YYYY-MM-dd`) ## Check AUTO_INCREMENT value SHOW TABLE STATUS FROM `prism_prepro` WHERE `name` LIKE 'breeding_contract_extra'; -
logbasex revised this gist
Oct 30, 2020 . 1 changed file with 62 additions and 21 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 @@ -62,50 +62,91 @@ `NOW()` **Selecting records:** `SELECT * FROM [table];` **Explain records:** `EXPLAIN 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: `<`, `>`, `!=`; 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];` **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];` **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.) **Delete all records in a table:** `truncate table [table];` **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];` **Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)):** `mysqldump -u [username] -p [database] > db_backup.sql` Use `--lock-tables=false` option for locked tables (more info [here](http://stackoverflow.com/a/104628/1815847)). **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;` -
logbasex revised this gist
Oct 30, 2020 . 1 changed file with 46 additions and 14 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 @@ -6,33 +6,61 @@ - [Sequel Pro](http://www.sequelpro.com/) (abandoned) ## Commands **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];` **Determine what database is in use:** `select database();` **Show all tables:** `show tables;` **Show table structure:** `describe [table];` **List all indexes on a table:** `show index from [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];` @@ -138,11 +166,15 @@ SET FOREIGN_KEY_CHECKS=1; -- to re-enable them SHOW TABLE STATUS FROM `prism_prepro` WHERE `name` LIKE 'breeding_contract_extra'; #### LAST_INSERT_ID() -- --------------NOT WORK IF INSERT ID------------------ `insert into bonus_scheme (id, code, description, media_id) values (32, 'TASBOW', 'TASBRED - OWNER ONLY Bonus Scheme', null);` `select LAST_INSERT_ID();` -- --------------RUN TWO QUERIES AT ONCE----------------- `insert into bonus_scheme (code, description, media_id) values ('TASBOW', 'TASBRED - OWNER ONLY Bonus Scheme', null);` `select LAST_INSERT_ID();` ## How to reset AUTO_INCREMENT -
logbasex revised this gist
Oct 30, 2020 . 1 changed file with 5 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 @@ -137,12 +137,13 @@ SET FOREIGN_KEY_CHECKS=1; -- to re-enable them SHOW TABLE STATUS FROM `prism_prepro` WHERE `name` LIKE 'breeding_contract_extra'; #### LAST_INSERT_ID() -- --------------NOT WORK IF INSERT ID------------------ `insert into bonus_scheme (id, code, description, media_id) values (32, 'TASBOW', 'TASBRED - OWNER ONLY Bonus Scheme', null);` `select LAST_INSERT_ID();` -- --------------RUN TWO QUERIES AT ONCE----------------- `insert into bonus_scheme (code, description, media_id) values ('TASBOW', 'TASBRED - OWNER ONLY Bonus Scheme', null);` `select LAST_INSERT_ID();` ## How to reset AUTO_INCREMENT -
logbasex revised this gist
Oct 30, 2020 . 1 changed file with 19 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 @@ -129,4 +129,22 @@ Convert milisecond to datetime: `select from_unixtime(1591711200000/1000);` SET FOREIGN_KEY_CHECKS=0; -- to disable foreign check SET FOREIGN_KEY_CHECKS=1; -- to re-enable them # Problems ## Check AUTO_INCREMENT value SHOW TABLE STATUS FROM `prism_prepro` WHERE `name` LIKE 'breeding_contract_extra'; #### LAST_INSERT_ID() insert into bonus_scheme (id, code, description, media_id) values (32, 'TASBOW', 'TASBRED - OWNER ONLY Bonus Scheme', null); -- not work with id select LAST_INSERT_ID(); -- --------------RUN TWO QUERIES AT ONCE----------------- insert into bonus_scheme (code, description, media_id) values ('TASBOW', 'TASBRED - OWNER ONLY Bonus Scheme', null); select LAST_INSERT_ID(); ## How to reset AUTO_INCREMENT ALTER TABLE tablename AUTO_INCREMENT = value; -
logbasex revised this gist
Jul 3, 2020 . 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 @@ -128,4 +128,5 @@ Convert milisecond to datetime: `select from_unixtime(1591711200000/1000);` ## Another SET FOREIGN_KEY_CHECKS=0; -- to disable foreign check SET FOREIGN_KEY_CHECKS=1; -- to re-enable them -
logbasex revised this gist
Jul 3, 2020 . 1 changed file with 6 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 @@ -123,4 +123,9 @@ Like concat: `SELECT url FROM media where url like concat('%','youtube','%');` Find mysql version: `SELECT @@version;` Convert milisecond to datetime: `select from_unixtime(1591711200000/1000);` ## Another SET FOREIGN_KEY_CHECKS=0; -- to disable foreign check SET FOREIGN_KEY_CHECKS=1; -- to re-enable them -
logbasex revised this gist
Jun 10, 2020 . 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 @@ -121,4 +121,6 @@ View as another date format: `SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') ## MySQL function Like concat: `SELECT url FROM media where url like concat('%','youtube','%');` Find mysql version: `SELECT @@version;` Convert milisecond to datetime: `select from_unixtime(1591711200000/1000);` -
logbasex revised this gist
May 28, 2020 . 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 @@ -120,4 +120,5 @@ View as another date format: `SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') ## MySQL function Like concat: `SELECT url FROM media where url like concat('%','youtube','%');` Find mysql version: `SELECT @@version;` -
logbasex revised this gist
May 28, 2020 . 1 changed file with 2 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 @@ -119,4 +119,5 @@ Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'l View as another date format: `SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM table_reference;` (Default date format of MySQL is: `YYYY-MM-dd`) ## MySQL function Like concat: `SELECT url FROM media where url like concat('%','youtube','%');` Find mysql version: `SELECT @@version;` -
logbasex revised this gist
May 15, 2020 . 1 changed file with 0 additions and 10 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,15 +1,5 @@ # MySQL ## Tools - [TablePlus](https://tableplus.io/) - [DataGrip](https://www.jetbrains.com/datagrip/) -
logbasex revised this gist
May 15, 2020 . 1 changed file with 4 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 @@ -126,4 +126,7 @@ Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'l `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646)) ## Common problem View as another date format: `SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM table_reference;` (Default date format of MySQL is: `YYYY-MM-dd`) ## MySQL function Like concat: `SELECT url FROM media where url like concat('%','youtube','%');` -
logbasex revised this gist
May 14, 2020 . 1 changed file with 4 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 @@ -123,4 +123,7 @@ Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';` Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';` ## Find out the IP Address of the Mysql Host `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646)) ## Common problem Wiew as another date format: `SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM table_reference;` (Default date format of MySQL is: `YYYY-MM-dd`) -
hofmannsven revised this gist
May 12, 2020 . 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 @@ -3,6 +3,7 @@ ## Getting started - http://www.sqlteaching.com/ - https://www.codecademy.com/learn/learn-sql - https://www.codecademy.com/catalog/language/sql ### Related tutorials - [MySQL-CLI](https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P) -
hofmannsven revised this gist
May 12, 2020 . 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 @@ -39,7 +39,7 @@ 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()` -
hofmannsven revised this gist
Jul 15, 2019 . 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 @@ -7,7 +7,7 @@ ### Related tutorials - [MySQL-CLI](https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P) - [Analyzing Business Metrics](https://www.codecademy.com/learn/sql-analyzing-business-metrics) - [SQL joins infografic](https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins) ## Tools - [TablePlus](https://tableplus.io/) -
hofmannsven revised this gist
Mar 21, 2019 . 1 changed file with 3 additions and 3 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,15 +1,15 @@ # MySQL ## Getting started - http://www.sqlteaching.com/ - https://www.codecademy.com/learn/learn-sql ### Related tutorials - [MySQL-CLI](https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P) - [Analyzing Business Metrics](https://www.codecademy.com/learn/sql-analyzing-business-metrics) - [SQL joins infografic](https://lh4.googleusercontent.com/-RdjzcoAwBYg/UxTXWGJHgoI/AAAAAAAACrs/Gqbu6zyksgo/w852-h670/sql-joins.jpg) ## Tools - [TablePlus](https://tableplus.io/) - [DataGrip](https://www.jetbrains.com/datagrip/) - [Sequel Pro](http://www.sequelpro.com/) (abandoned) -
hofmannsven revised this gist
Mar 21, 2019 . 1 changed file with 10 additions and 24 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,23 +1,20 @@ # MySQL ## Getting started: - http://www.sqlteaching.com/ - https://www.codecademy.com/learn/learn-sql Related tutorials: - [MySQL-CLI](https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P) - [Analyzing Business Metrics](https://www.codecademy.com/learn/sql-analyzing-business-metrics) - [SQL joins infografic](https://lh4.googleusercontent.com/-RdjzcoAwBYg/UxTXWGJHgoI/AAAAAAAACrs/Gqbu6zyksgo/w852-h670/sql-joins.jpg) Tools: - [TablePlus](https://tableplus.io/) - [DataGrip](https://www.jetbrains.com/datagrip/) - [Sequel Pro](http://www.sequelpro.com/) (abandoned) ## Commands Access monitor: `mysql -u [username] -p;` (will prompt for password) Show all databases: `show databases;` @@ -93,10 +90,7 @@ Import a database dump (more info [here](http://stackoverflow.com/a/21091197/181 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];` @@ -111,10 +105,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];` ## 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];` @@ -123,17 +114,12 @@ Combine rows from different tables but do not require the join condition: `SELEC Rename column or table using an _alias_: `SELECT [table1].[column] AS '[value]', [table2].[column] AS '[value]' FROM [table1], [table2];` ## Users 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';` ## Find out the IP Address of the Mysql Host `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646)) -
hofmannsven revised this gist
Mar 14, 2019 . No changes.There are no files selected for viewing
-
hofmannsven revised this gist
Jul 4, 2017 . 1 changed file with 5 additions and 5 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 @@ -6,13 +6,13 @@ Getting started: - https://www.codecademy.com/courses/learn-sql Related tutorials: - [MySQL-CLI](https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P) - [Analyzing Business Metrics](https://www.codecademy.com/learn/sql-analyzing-business-metrics) - [SQL joins infografic](https://lh4.googleusercontent.com/-RdjzcoAwBYg/UxTXWGJHgoI/AAAAAAAACrs/Gqbu6zyksgo/w852-h670/sql-joins.jpg) Tools: - [DataGrip](https://www.jetbrains.com/datagrip/) - [Sequel Pro](http://www.sequelpro.com/) Commands -
hofmannsven revised this gist
Jul 28, 2016 . 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 @@ -28,10 +28,14 @@ Create new database: `create database [database];` Select database: `use [database];` Determine what database is in use: `select database();` Show all tables: `show tables;` Show table structure: `describe [table];` List all indexes on a table: `show index from [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);` @@ -71,6 +75,8 @@ 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.) Delete all records in a table: `truncate table [table];` Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];` Deleting tables: `DROP TABLE [table];` -
hofmannsven revised this gist
Mar 15, 2016 . 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 @@ -10,6 +10,10 @@ Related tutorials: - Analyzing Business Metrics: https://www.codecademy.com/learn/sql-analyzing-business-metrics - SQL joins infografic: http://cd64.de/sql-joins Tools: - DataGrip: https://www.jetbrains.com/datagrip/ - Sequel Pro: http://www.sequelpro.com/ Commands ----------- -
hofmannsven revised this gist
Feb 26, 2016 . 1 changed file with 4 additions and 3 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 @@ -5,9 +5,10 @@ Getting started: - http://www.sqlteaching.com/ - https://www.codecademy.com/courses/learn-sql Related tutorials: - MySQL-CLI: https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P - Analyzing Business Metrics: https://www.codecademy.com/learn/sql-analyzing-business-metrics - SQL joins infografic: http://cd64.de/sql-joins Commands -
hofmannsven revised this gist
Feb 5, 2016 . 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 @@ -125,4 +125,4 @@ Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'l Find out the IP Address of the Mysql Host ----------- `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646)) -
hofmannsven revised this gist
Feb 5, 2016 . 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 @@ -125,4 +125,4 @@ Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'l Find out the IP Address of the Mysql Host ----------- `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646), contribution @junrillg)
NewerOlder