MYSQLコマンドのクリーンアップ

8513 ワード

SELECT
選択した列のitemを返します.
SELECT * FROM tablename 	# 전체 item 반환
SELECT column1, column2 FROM tablename
ORDER BY
選択した条件で昇順、降順でソート
SELECT * FROM tablename ORDER BY column1 ASC		# c1 기준 오름차순 정렬
SELECT * FROM tablename ORDER BY column2 DESC		# c2 기준 내림차순 정렬
SELECT * FROM tablename ORDER BY column1 ASC, column2 DESC
WHERE
グループ化前に条件を満たすアイテムを返す
SELECT * FROM tablename WHERE column1='cond' 		# c1이 cond인 item 반환 
SELECT TOP N
topn個のみ選択
SELECT * FROM tablename ORDER BY column1 ASC LIMIT n	# n개만 반환
MIN MAX
SELECT MIN(column1) FROM tablename 
SELECT MAX(column2) FROM tablename 
COUNT
カウントは/NuLL値を返し、無視
SELECT COUNT(*) FROM tablename 				# 전채 행 갯수
SELECT COUNT(column1) FROM tablename 		# c1 데이터 갯수
DISTINCT
重複しない
SELECT DISTINCT(column1) FROM tablename
SELECT COUNT(DISTINCT(column1)) FROM tablename 
GROUP BY
SELECT column1 COUNT(*) FROM tablename GROUP BY(column1) 	
# c1 기준으로 그룹화 해서 각 item이 몇 개씩 있나 반환
HAVING
グループ化後に使用するWHERE文
SELECT column1 COUNT(*) FROM tablename GROUP BY(column1) HAVING COUNT(*)>1
# c1 기준으로 그룹화 해서 그룹의 아이템이 2개 이상일 때만 각 item이 몇 개씩 있나 반환
IS AS
SELECT * FROM tablename WHERE NOT (column1) IS NULL
SELECT colum1 AS c1, colum2 AS c2 FROM tablename
DATETIME
特定のデータのみを抽出するには、次の方法を使用します.
YEAR(DATETIME)
MONTH(DATETIME)
HOUR(DATETIME)
COALESCE
NULL値の置換
SELECT COALESCE(column1, 'no') FROM tableNAME
SELECT COALESCE(column1, column2, 'nono') FROM tableNAME
# c1이 null이면 c2, c2도 null이면 'nono'
IN
SELECT column1 FROM tableNAME
WHERE column1 IN ('case1', 'case2', 'case3')
LIKE
特定の文字列が含まれているかどうかを検索(%一般*と同じ)
SELECT column1 FROM tableNAME
WHERE LOWER(column1) LIKE '%something%'
IF
IF(条件、「真」、「偽」)
JOIN
  • LEFT JOIN
  • JOIN
    本当に純粋に重なる要素
  • を返します.
  • RIGHT JOIN