SELECT NAME
FROM ANIMAL_INS
ORDER BY DATETIME
LIMIT 1
SELECT *
FROM 테이블명
LIMIT 0,3 0번부터 3개 뽑기
SELECT COUNT(*)
FROM ANIMAL_INS
SELECT UPPER(MEMBER_ID)
FROM REST_REVIEW
-- MIN09125@NAVER.COM
SELECT COUNT(DISTINCT NAME)
FROM ANIMAL_INS
SELECT CONCAT('문자열', ' ', '붙이기')
FROM 테이블
-- 문자열 붙이기
SELECT ANIMAL_TYPE, count(*)
FROM ANIMAL_INS
GROUP BY ANIMAL_TYPE
ORDER BY ANIMAL_TYPE ASC
SELECT NAME, COUNT(NAME)
FROM ANIMAL_INS
GROUP BY NAME
HAVING COUNT(NAME) > 1
ORDER BY NAME
SELECT IF(10>5, '크다', '작다') AS RESULT;
SELECT HOUR(DATETIME) , COUNT(HOUR(DATETIME))
FROM ANIMAL_OUTS
WHERE 9 <= HOUR(DATETIME) and HOUR(DATETIME) <= 19
GROUP BY HOUR(DATETIME)
ORDER BY HOUR(DATETIME)
SET
: 어떤 변수에 특정 값을 할당할때 쓰는 명령어SET
사용시 대입 연산자 =
를 사용하고 그 외에는 :=
를 사용해야 한다HOUR
변수를 -1로 선언하여서 22까지 +1씩 더해주었다. -1부터 더했기 때문에 0부터 23까지 생성이 된다.ANIMAL_OUTS
테이블에 있는 DATETIME
변수와 @HOUR
변수가 동일한 순간 카운트를 진행한다.SET @HOUR = -1;
SELECT (@HOUR := @HOUR +1) AS HOUR,
(SELECT COUNT(HOUR(DATETIME))
FROM ANIMAL_OUTS
WHERE HOUR(DATETIME)=@HOUR) AS COUNT
FROM ANIMAL_OUTS
WHERE @HOUR < 23;
SELECT ANIMAL_ID
FROM ANIMAL_INS
WHERE NAME IS NOT NULL
ORDER BY ANIMAL_ID
NAME
이 NULL일 때 No name 삽입SELECT ANIMAL_TYPE, IFNULL(NAME, 'No name'), SEX_UPON_INTAKE
FROM ANIMAL_INS
ORDER BY ANIMAL_ID
SELECT ANIMAL_OUTS.ANIMAL_ID, ANIMAL_OUTS.NAME
FROM ANIMAL_OUTS
LEFT OUTER JOIN ANIMAL_INS
ON ANIMAL_OUTS.ANIMAL_ID = ANIMAL_INS.ANIMAL_ID
WHERE ANIMAL_INS.ANIMAL_ID IS NULL
ORDER BY ANIMAL_OUTS.ANIMAL_ID
SELECT ANIMAL_ID, NAME
FROM ANIMAL_OUTS
WHERE ANIMAL_ID NOT IN (SELECT ANIMAL_ID FROM ANIMAL_INS)
ORDER BY ANIMAL_ID
SELECT ANIMAL_INS.ANIMAL_ID, ANIMAL_INS.NAME
FROM ANIMAL_INS LEFT OUTER JOIN ANIMAL_OUTS
ON ANIMAL_INS.ANIMAL_ID = ANIMAL_OUTS.ANIMAL_ID
WHERE ANIMAL_INS.DATETIME > ANIMAL_OUTS.DATETIME
ORDER BY ANIMAL_INS.DATETIME
SELECT ANIMAL_INS.ANIMAL_ID, ANIMAL_INS.NAME
FROM ANIMAL_INS , ANIMAL_OUTS
WHERE ANIMAL_INS.ANIMAL_ID = ANIMAL_OUTS.ANIMAL_ID
AND ANIMAL_INS.DATETIME > ANIMAL_OUTS.DATETIME
ORDER BY ANIMAL_INS.DATETIME
SELECT INS.NAME, INS.DATETIME
FROM ANIMAL_INS INS
LEFT OUTER JOIN ANIMAL_OUTS OUTS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE OUTS.ANIMAL_ID IS NULL
ORDER BY INS.DATETIME
LIMIT 3
SELECT INS.NAME, INS.DATETIME
FROM ANIMAL_INS INS
WHERE ANIMAL_ID NOT IN (SELECT ANIMAL_ID FROM ANIMAL_OUTS)
ORDER BY INS.DATETIME
LIMIT 3
SELECT INS.ANIMAL_ID, INS.ANIMAL_TYPE, INS.NAME
FROM ANIMAL_INS INS LEFT OUTER JOIN ANIMAL_OUTS OUTS
ON INS.ANIMAL_ID = OUTS.ANIMAL_ID
WHERE OUTS.SEX_UPON_OUTCOME != INS.SEX_UPON_INTAKE
SELECT ANIMAL_INS.ANIMAL_ID, ANIMAL_INS.ANIMAL_TYPE, ANIMAL_INS.NAME
FROM ANIMAL_INS, ANIMAL_OUTS
WHERE ANIMAL_INS.ANIMAL_ID = ANIMAL_OUTS.ANIMAL_ID
AND ANIMAL_OUTS.SEX_UPON_OUTCOME != ANIMAL_INS.SEX_UPON_INTAKE
SELECT ANIMAL_ID, NAME, SEX_UPON_INTAKE
FROM ANIMAL_INS
WHERE NAME IN ("Lucy","Ella","Pickle","Rogan","Sabrina","Mitty")
ORDER BY ANIMAL_ID
SELECT *
FROM PRODUCTS
WHERE PRICE BETWEEN 10 AND 20
SELECT ANIMAL_ID, NAME
FROM ANIMAL_INS
WHERE NAME LIKE '%EL%' AND ANIMAL_TYPE = "Dog"
ORDER BY NAME
SELECT LEFT(PRODUCT_CODE,2) AS CATEGORY, COUNT(*) AS PRODUCTS
FROM PRODUCT
GROUP BY CATEGORY
ORDER BY CATEGORY ASC
SELECT ANIMAL_ID, NAME,
CASE
WHEN (SEX_UPON_INTAKE LIKE "Neutered%"
OR SEX_UPON_INTAKE LIKE "Spayed%") THEN "O"
ELSE "X"
END AS 중성화
FROM ANIMAL_INS
ORDER BY ANIMAL_ID
10 <= a < 0
(❌) 10 <= a and a < 0
(⭕️)SELECT
CASE
WHEN 0 <= PRICE AND PRICE < 10000 THEN 0
WHEN 10000 <= PRICE AND PRICE < 20000 THEN 10000
WHEN 20000 <= PRICE AND PRICE < 30000 THEN 20000
WHEN 30000 <= PRICE AND PRICE < 40000 THEN 30000
WHEN 40000 <= PRICE AND PRICE < 50000 THEN 40000
WHEN 50000 <= PRICE AND PRICE < 60000 THEN 50000
WHEN 60000 <= PRICE AND PRICE < 70000 THEN 60000
WHEN 70000 <= PRICE AND PRICE < 80000 THEN 70000
WHEN 80000 <= PRICE AND PRICE < 90000 THEN 80000
END AS PRICE_GROUP
, COUNT(*) AS PRODUCTS
FROM PRODUCT
GROUP BY PRICE_GROUP
ORDER BY PRICE_GROUP
SELECT OUTS.ANIMAL_ID, OUTS.NAME
FROM ANIMAL_OUTS OUTS LEFT OUTER JOIN ANIMAL_INS INS
ON OUTS.ANIMAL_ID = INS.ANIMAL_ID
ORDER BY OUTS.DATETIME - INS.DATETIME DESC
LIMIT 2
SELECT ANIMAL_OUTS.ANIMAL_ID, ANIMAL_OUTS.NAME
FROM ANIMAL_INS, ANIMAL_OUTS
WHERE ANIMAL_INS.ANIMAL_ID = ANIMAL_OUTS.ANIMAL_ID
ORDER BY ANIMAL_OUTS.DATETIME - ANIMAL_INS.DATETIME DESC
LIMIT 2
SELECT ANIMAL_ID, NAME, DATE_FORMAT(DATETIME,'%Y-%m-%d') AS 날짜
FROM ANIMAL_INS
ORDER BY ANIMAL_ID
DATE_OF_BIRTH = 1992-03-16 00:00:00
여기에서 3월만 뽑아내기YEAR()
, DAY()
SELECT MEMBER_ID, MEMBER_NAME, GENDER, DATE_OF_BIRTH
FROM MEMBER_PROFILE
WHERE MONTH(DATE_OF_BIRTH) = 3
AND TLNO IS NOT NULL
AND GENDER = 'W'
ORDER BY MEMBER_ID ASC
데이터가 2022-05-04
이런 형식일 때 2022년 5월 데이터만 뽑는 방법
YEAR(PRODUCE_DATE) = 2022 AND MONTH(PRODUCE_DATE) = 5
PRODUCE_DATE BETWEEN "2022-05-01" AND "2022-05-31"
WHERE PRODUCE_DATE LIKE '2022-05%'
PRODUCE_DATE LIKE '2022-05-%'
DATEDIFF(OUT_DATE,'2022-05-01')
: OUT_DATE와 '2022-05-01' 날짜 차이를 int형으로 반환SELECT ORDER_ID, PRODUCT_ID, DATE_FORMAT(OUT_DATE,'%Y-%m-%d') AS OUT_DATE,
CASE
WHEN DATEDIFF(OUT_DATE,'2022-05-01') > 0 THEN '출고대기'
WHEN DATEDIFF(OUT_DATE,'2022-05-01') <= 0 THEN '출고완료'
WHEN OUT_DATE IS NULL THEN '출고미정'
END 출고여부
FROM FOOD_ORDER
ORDER BY ORDER_ID ASC
https://moonsbeen.tistory.com/375
https://paris-in-the-rain.tistory.com/100
https://hello-i-t.tistory.com/119
https://hyunsix.tistory.com/entry/SQL-SQL-%EC%BD%94%EB%94%A9%ED%85%8C%EC%8A%A4%ED%8A%B8-%EB%8C%80%EB%B9%84-%ED%95%A8%EC%88%98-%EC%A0%95%EB%A6%AC
https://dallae7.tistory.com/121