본문 바로가기

프로그래밍 언어

[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_.. 더보기
[MySQL] 중복없이 데이터 가져오기: distinct use yh_db; select * from books; select * from yh_db.books; select * from 데이터베이스 이름.테이블 이름 -- 이때는 use 안써도 됨. .(점)의 뜻은 '~의'라는 뜻 insert into books (title, author_fname,author_lname,released_year,stock_quantity,pages) values ('10% Happier','Dan','Harris',2014, 29, 256), ('fake_book','Freida','Harris',2001,287,428), ('Lincon In The Bardo','George','Saunders', 2017, 111, 388); select * from books; 중복없.. 더보기
[MySQL] concat, concat_ws, substring, substr, replace, reverse, char_length, upper, lower 하기 텍스트 입력(앞에 use yh_db 작성하고) INSERT INTO books (title, author_fname, author_lname, released_year, stock_quantity, pages) VALUES ('The Namesake', 'Jhumpa', 'Lahiri', 2003, 32, 291), ('Norse Mythology', 'Neil', 'Gaiman',2016, 43, 304), ('American Gods', 'Neil', 'Gaiman', 2001, 12, 465), ('Interpreter of Maladies', 'Jhumpa', 'Lahiri', 1996, 97, 198), ('A Hologram for the King: A Novel', 'Dave', 'Eg.. 더보기
[MySQL] delete from ..where, select ..from -- 고양이 이름이 Misty 인 고양이의 나이를 14로 바꾸세요. update cats set age = 14 where name = 'Misty'; -- 이름이 'Jackson'인 고양이의 이름을 'Jack'으로 바꾸세요. update cats set name = 'Jack' where name = 'Jackson'; 데이터 삭제하는 방법 delete from cats -- 밑에 조건 없이 요 줄만 쓰면, 데이터 전체를 삭제한다는 뜻이니, 조심하길 where name = 'Egg'; select *from cats; -- 고양이의 나이가 4살인 고양이를 데이터를 삭제하세요. delete from cats where age = 4; select *from cats; – *: 전체 데이터를 가져온다는 뜻.. 더보기