subquery, group by, order by

2022. 12. 13. 15:06개인적인 공부/Database 복습

SELECT
	title,
	COUNT(1)
FROM
	test_mst
GROUP BY	
	title;
	
SELECT
	content,
	COUNT(1)
FROM
	test_mst
GROUP BY
	content;

SELECT 
	writer_id,
	COUNT(1)
FROM
	test_mst
GROUP BY
	writer_id;
	
SELECT
	writer_id,
	COUNT(writer_id)
FROM
	test_mst
GROUP BY
	writer_id;
	

SELECT
	*,
	(SELECT COUNT(1)FROM test_mst tm2 WHERE tm2.writer_id = tm.writer_id)
FROM
	test_mst tm;
	
SELECT
	tm.title AS '제목',
	tm.content AS '내용',
	tm.writer_id AS '작가',
	writer_count AS ' 작성횟수'
FROM
	test_mst tm
	LEFT OUTER JOIN (SELECT writer_id,
			COUNT(1) AS writer_count
		FROM test_mst 
		GROUP BY 
			writer_id) wc ON(wc.writer_id = tm.writer_id)
ORDER BY	
	tm.writer_id desc;
			

SELECT
	tm.title AS'제목',
	tm.content AS '내용',
	tm.writer_id AS '작가',
	title_count AS '중복제목'
FROM 
	test_mst tm
	LEFT OUTER JOIN (SELECT title,
		COUNT(1) AS title_count
	FROM test_mst
	GROUP by
		title) tc ON(tc.title = tm.title)
ORDER BY
	tm.title desc;

SELECT
	tm.title AS'제목',
	tm.content AS '내용',
	tm.writer_id AS '작가',
	content_count AS '내용중복'
FROM
	test_mst tm
	LEFT OUTER JOIN (SELECT content,
		COUNT(1) AS content_count
	FROM test_mst
	GROUP BY	
		content) cc ON(cc.content = tm.content);

'개인적인 공부 > Database 복습' 카테고리의 다른 글

java delete database  (0) 2022.12.15
java select database  (0) 2022.12.15
java로 데이터 베이스안 정보 입력  (0) 2022.12.15
database sql  (0) 2022.12.12