DELIMITER $$
DROP PROCEDURE IF EXISTS loopInsert$$
CREATE PROCEDURE loopInsert() 

BEGIN

DECLARE i INT DEFAULT 1;



WHILE i <= 500 DO 

insert into test.whisky(name, origin) values('asdfasdf', 'asdfasdf');

SET i = i + 1;

END WHILE;

END$$

DELIMITER $$

CALL loopInsert;

'데이터베이스' 카테고리의 다른 글

Various queries 1  (4) 2019.12.03

1. select substring(datetime, 12, 2) as hour, count(datetime) 
from animal_outs
group by hour
having hour
between 9 and 19

 

--- if you want to only get hours using datetime, use a substring where you can define the start point and the end point of a certain column/data. 

datetime is formatted as YYYY-MM-DD HH:MI:SS, so the twelfth starting value is H while ending is also H resulting in hours only

 

-- extract(hour from datetime) much more simple i think also works as well

 

2. select a1.hour, ifnull(a2.count, 0)
from ( select 0 as hour
     union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 union select 13 union select 14 union select 15 union select 16 union select 17 union select 18 union select 19 union select 20 union select 21 union select 21 union select 22 union select 23) a1
     left join  (select hour(datetime) as hour, count(*) as count 
                 from animal_outs
                 group by hour) 
                 as a2 on a2.hour = a1.hour

 

--still baffled 

 

3.SELECT animal_type, ifnull(name, 'No name')name, sex_upon_intake 
from animal_ins

 

--use ifnull no make a exception, like number 2

 

4. -- 코드를 입력하세요
SELECT animal_id, name, sex_upon_intake
from animal_ins
where name in('lucy', 'ella', 'pickle', 'rogan', 'sabrina', 'mitty')

 

--if you have multiple condition that needs to be searched, using in like this would work

 

5. SELECT animal_id, name, 
    CASE WHEN sex_upon_intake like 'Intact%'
        THEN 'X'
        ELSE 'O' 
    END as 중성화
from animal_ins

 

--case end and when then else using like wildcard

 

SELECT animal_id, name, IF(sex_upon_intake like "Intact%", 'X', 'O')
from animal_ins
order by animal_id;

 

--here used a if like a 삼항연산자 ternary operation

 

6. -- 코드를 입력하세요
SELECT ai.animal_id, ai.name 
from animal_ins ai 
left join animal_outs ao
on ai.animal_id = ao.animal_id 
order by ao.datetime - ai.datetime desc
limit 2

 

-- 데이트 차이 제일 큰 2동물 선택

 

7. SELECT animal_id, name, date_format(datetime, '%Y-%m-%d') as 날짜 
from animal_ins
order by animal_id asc

 

or substring(datetime, 1, 10)

 

--prints year month day 

'데이터베이스' 카테고리의 다른 글

mysql notes 1 loop  (0) 2020.05.27

+ Recent posts