/
bear
/
my_snippets
Обзор
Документация
Войти
/
bear
/
my_snippets
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
data/sql_snippets.xml
102 строки
8 KB
Nikita Kalitin
add(sql): cheat with sum(if(...))
27 фев 2020, 12:48
27 фев 2020, 12:48
7e15819
Код
Авторство
О чём код?
<templateSet group="sql_snippets"> <template name="sql_left_join" value="-- SELECT a, b FROM DB.tableOne LEFT JOIN DB.tableTwo ON tableOne.id = tableTwo.id; --" description="объединение таблиц слева ( А или (А и Б) ) <{002.png}>" toReformat="true" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> <template name="sql_inner_join" value="-- SELECT * FROM TableA A INNER JOIN TableB B ON A.key = B.key --" description="пересечение таблиц ( А и Б ) <{003.png}>" toReformat="false" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> <template name="sql_right_join" value="-- SELECT * FROM TableA A RIGHT JOIN TableB B ON A.key = B.key --" description="объединение таблиц справа (Б или (А и Б)) <{004.png}>" toReformat="false" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> <template name="sql_full_join" value="-- SELECT * FROM TableA A FULL OUTER JOIN TableB B ON A.key = B.key --" description="полное объединение таблиц ( А или Б ) <{001.png}>" toReformat="false" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> <template name="sql_pirs_join" value="-- SELECT * FROM TableA A FULL OUTER JOIN TableB B ON A.key = B.key --" description="стрелка пирса ? !(А и Б)<{005.png}>" toReformat="false" toShortenFQNames="true" /> <template name="REGEXP_ex1" value="###------------------------------------- SELECT * from <table.column> AS ee where ee.<column> REGEXP "^Название стоблца №.*[0-9]$"; ###-------------------------------------" description="example REGEXP 1" toReformat="false" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> <template name="sql_add_right_for_dump" value="GRANT USAGE ON *.* TO '<userName>'@'localhost' IDENTIFIED BY '<userpassword>'; GRANT SELECT, LOCK TABLES ON `<dbName>`.* TO '<userName>'@'localhost'; GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON `<dbName>`.* TO '<userName>'@'localhost'; GRANT TRIGGER ON `reestr`.* TO '<dbName>'@'localhost';" description="дать права пользоватею на использование mysqldump для снятия дампа бд" toReformat="false" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> <template name="strict_mode_to_null" value="#to show current settings SELECT @@GLOBAL.sql_mode;performance_schema #to set strict mode to null SET @@GLOBAL.sql_mode=''" description="disable_strict_mode" toReformat="false" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> <template name="col" value="$col$ $type$ $null$$END$" description="new column definition" toReformat="true" toShortenFQNames="false"> <variable name="col" expression="" defaultValue=""col"" alwaysStopAt="true" /> <variable name="type" expression="" defaultValue=""int"" alwaysStopAt="true" /> <variable name="null" expression="" defaultValue=""not null"" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="ins" value="insert into $table$ ($columns$) values ($END$);" description="insert rows into a table" toReformat="true" toShortenFQNames="false"> <variable name="table" expression="complete()" defaultValue="" alwaysStopAt="true" /> <variable name="columns" expression="complete()" defaultValue="" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="sel" value="select * from $table$$END$;" description="select all rows from a table" toReformat="true" toShortenFQNames="false"> <variable name="table" expression="complete()" defaultValue="" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="selc" value="select count(*) from $table$ $alias$ where $alias$.$END$;" description="select the number of specific rows in a table" toReformat="true" toShortenFQNames="false"> <variable name="table" expression="complete()" defaultValue="" alwaysStopAt="true" /> <variable name="alias" expression="complete()" defaultValue=""alias"" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="selw" value="select * from $table$ $alias$ where $alias$.$END$;" description="select specific rows from a table" toReformat="true" toShortenFQNames="false"> <variable name="table" expression="complete()" defaultValue="" alwaysStopAt="true" /> <variable name="alias" expression="complete()" defaultValue=""alias"" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="tab" value="create table $table$ ( $col$ $type$ $null$$END$ );" description="new table definition" toReformat="true" toShortenFQNames="false"> <variable name="table" expression="" defaultValue=""new_table"" alwaysStopAt="true" /> <variable name="col" expression="" defaultValue=""col"" alwaysStopAt="true" /> <variable name="type" expression="" defaultValue=""int"" alwaysStopAt="true" /> <variable name="null" expression="" defaultValue=""not null"" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="upd" value="update $table_name$ set $col$ = $value$ where $END$;" description="update values in a table" toReformat="true" toShortenFQNames="false"> <variable name="table_name" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="col" expression="complete()" defaultValue="" alwaysStopAt="true" /> <variable name="value" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="read_from_schema" value="#-------------------------------- select `column_name`, `column_type`, `column_default`, `column_comment` from `information_schema`.`COLUMNS` where `table_name` = '$TABLE_NAME$' and `table_schema` = '$DB_NAME$'; #--------------------------------" description="read information about column type and comment from schema" toReformat="false" toShortenFQNames="true"> <variable name="TABLE_NAME" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="DB_NAME" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="SQL" value="true" /> </context> </template> <template name="sql_truk" value="/*---------------------------------------------------*/ SELECT FIO_1, 		 SUM(if(ee.id_etap = 1,1,0)) AS 'Отчёт #-1', 		 SUM(if(ee.id_etap = 2,1,0)) AS 'Отчёт #-2', 		 SUM(if(ee.id_etap = 3,1,0)) AS 'Отчёт #-3', 		 SUM(if(ee.id_etap = 4,1,0)) AS 'Отчёт #-4', 		 id_etap, id_user, id_work FROM dbtest.table1 AS ee INNER JOIN ( SELECT id_user, CONCAT(family,' ',first_name , ' ', second_name, ' ') AS FIO FROM dbtest.users) AS qq ON ee.id_user = qq.id_user WHERE ee.id_etap IN (1, 2, 3, 4) GROUP BY id_user ORDER BY id_user /*---------------------------------------------------*/" description="count by diff columns" toReformat="false" toShortenFQNames="true"> <context> <option name="SQL" value="true" /> </context> </template> </templateSet>