본문 바로가기

프로그래밍 언어/MySQL

[MySQL] ~인것 가져오기: where, and, or, between…and, in(), case when …then.. else… end ~인것 가져오기 : where, and, or, between…and, in(), case when …then.. else… end -- 오늘 가장 중요한 이야기. -- ~인것 가져오기 -- 연도가 2017인 데이터를 가져오시오 select * from books where released_year = 2017; select * from books where released_year = 2010; -- 년도가 2017년이 아닌 데이터만 가져오시오. select * from books where released_year != 2017; -- author_lname이 Harris가 아닌 데이터만 가져오시오. select * from books where author_lname != 'Harris'; sele.. 더보기
[MySQL] 쿼리문으로 테이블 생성 및 현재 시간 기록 설정 쿼리문으로 테이블 생성 및 현재 시간 기록 설정 create table comments( id int unsigned not null Auto_increment primary key, content varchar(100), created_at timestamp default now() ); updated_at: ㄴ default/Expresstion: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP insert into comments (content) values ('이 사과 정말 맛있습니다.'); insert into comments (content) values ('진짜?'); insert into comments (content) values ('사과 진짜 맛있나.. 더보기
[MySQL] 시간 계산: datediff(), date_add(), interval + 시간 desc people2; insert into people2 (name,birthdate, birthtime, birthdt) values ('Padma','1988-11-11','10:07:35','1988-11-11 10:07:35'), ('Larry','1994-04-22','04:10:42','1994-04-22 04:10:42'); select * from people2; select name, year(birthdate) from people2; 해당 컬럼의 년도를 보여줌 select name, month(birthdate) from people2; 해당 컬럼의 월를 보여줌 select name, day(birthdate) from people2; 해당 컬럼의 날짜를 보여줌 select nam.. 더보기
[MySQL] 시간을 처리하는 방법:curdate(), curtime(), now() - 관련 튜토리얼: https://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm\ create a table desc people2; insert into people2 (name,birthdate, birthtime, birthdt) values ('Padma','1988-11-11','10:07:35','1988-11-11 10:07:35'), ('Larry','1994-04-22','04:10:42','1994-04-22 04:10:42'); select * from people2; 시간을 처리하는 방법:curdate(), curtime(), now() -- date, time, datetime -- date: YYYY-MM-DD -- ti.. 더보기
[MySQL] 최소값/최대값/평균/총합: min(), max(), avg(),sum() 최소값/최대값/평균: min(), max(), avg() -- 년도의 최소값은? select min(released_year) from books; -- 페이지수가 가장 큰 값은?? select *,max(pages) -- 전체 컬럼과 페이지 제일 큰 데이터를 가져온다. from books; -- 페이지수가 가장 큰 책의 제목은?? select *, title from books order by pages desc limit 1; -- 이 테이블의 책 페이지수 평균은?? select avg(pages) from books; 문제풀이: -- 각 작가별로, 자신이 쓴 책의 최소 년도는?? -- 작가의 성과 이름, 그리고 최소년도가 나오도록 가져오세요. -- 앞에는 컬럼이라는 뜻 select author_fn.. 더보기
[MySQL] 갯수 세기,카운트 함수: count()/ ~별로 그룹묶기: group by 갯수 세기 - 카운트 함수, count() - 데이터 갯수를 센다. - 데이터는 행이다. -- books 테이블의 데이터 갯수(책의 갯수)는?? select count(*) from books; -- 19개 -- author_fname의 갯수는? select count( distinct author_fname) from books; -- 책 제목에 the라고 들어있는 책은 몇개인가요? select count(*) from books where title like '%the%'; ~별로 그룹묶기: group by -- group by 옆에는 컬럼이름이 나와줘야한다. -- group by는, 컬럼 1개, 2개, 3개... 상관없이 여러개 가능 -- author_lname 별로, 몇권의 책을 썻는지, -- a.. 더보기
[MySQL] 단어 포함 여부 간단 검색: like/ 숫자 자리수 표시: 언더스코어(__) 단어 포함 여부 간단 검색: like -- 문자열 포함 (Search) : like 키워드 -- 내가 찾고자 하는 문자열이, 컬럼에 포함되어있는지 확인하는 방법 -- 포함하고 있는 단어, (간단한 검색기능) -- the%: the 오른쪽에 무슨 문자가 와도 상관없다 -> the로 시작하는 데이터 검색 -- %the: the 왼쪽에 무슨 문자가 와도 상관없다 -> the로 끝나는 데이터 검색 -- %: 무슨 문자가 와도 상관없다. 라는 뜻 -- %the%: 앞 뒤 무슨 문자가 와도 상관없으니, the가 포함된 데이터 검색 -- 대소문자 구분할 필요없이 알아서 본인이 찾아준다. -- 책 제목에 the라고 들어있는(%) 책만 가져오시오. select * from books where title like '%t.. 더보기
[MySQL] 정렬하기: order by/ 몇 행의 데이터 가져오기: limit 정렬하기: order by -- 데이터 정렬 order by, sql문의 맨 마지막에 작성하는 것. + 내가 정렬할 컬럼. -- 내림차순 정렬은 desc, 오름차순 정렬은 디폴트이므로, 아무것도 안쓰거나, asc라고 써주면 된다. -- 컬럼 이름 대신, 숫자를 써도 된다. 숫자는 첫 컬럼부터(1) 뜻한다. -- 책 제목으로 정렬하세요. select * from books order by title; select * from books order by title desc; select * from books order by title asc; -- 년도 내림차순으로 정렬하여, 년도와 책 제목을 가져오시오. select released_year,title from books order by released_.. 더보기