평균이 80~90점사이인 내용을 불러오는 avg()함수
조회 2,215 2026-08-18 등록
로그인하면 의견을 남길 수 있습니다.
아직 의견이 없습니다. 첫 의견을 남겨보세요.
게시글
group by 내에서 그룹으로 묶은 것에 대한 정렬하는 방법
select * from tablename as p where 1 and sidx_code in ( select max(sidx_code) from tablename where 1 and sidx_code=p.sidx_code ) group by p_title 위에 있는 필드 값을 접근하기 위해서는 위에 as p로 하고 p.sidx_code로 접근하면 된다.
rand()함수로 랜덤하게 정렬하기,select 내용를 랜덤으로 정렬하기
해당 레코드를 순서에 상관없이 랜덤으로 정렬할때는 select * from tablename order by rand() limit 0, 5 이렇게 하면 5개를 랜덤으로 정렬할수 있다 다른 정렬방식으로 최근게시글중 5개를 가저온다은에 그것을 랜던으로 호출하는 방법이다 예를 들어 idx_code -> 1,2,3,4,5,6,7,8,9,10 중에 10,9,8,7,6를 가저온후에 10~6까지를 랜덤으로 불러오는방법이다 (select * from tablename order by idx_code desc limit 0, 5) order by rand()
2개이상 중복되는 내용을 검색하는 지리문 having
2개이상 중복되는 데이터를 뽑아와야 하는경우가 있다 예를 들어 결제 내역중에 2번이상 결제한 사람을 뽑아오는 경우가 그렇다 select *, count(*) as count1 from 테이블명 where 1 group by 회원아이디필드 having count1 > 2 이렇게 하면 2번이상인 경우를 검색할수 있다
서브쿼리의 기본
select * from table where 1 and idx_code in (1,2,3,4,5) idx_code값중에 1,2,3,4,5를 가져온다 select * from table where 1 and idx_code not in (1,2,3,4,5) idx_code값중에 1,2,3,4,5아닌것을 가져온다 select * from table where 1 and idx_code in (select sidx_code table1 where 1) 검색된 내역중에 데이타 table1.sidx_code와 table.idx_code가 일치하는 내역만 호출
기간차이를 계산한다음에 정렬하는 방법
날짜별로 정렬하는것은 desc,asc를 통해서 충분히 가능하지만 기간에 대한 차이를 정렬할때는 기간차를 구한후 정렬을 해야한다 이때 계산하는 방법은 (unix_timestamp(edate)-unix_timestamp(sdate)) 방법을 사용하면된다 select *, (unix_timestamp(edate)-unix_timestamp(sdate)) as turm from tablename where 1 order by turm desc
mysql에서 날짜를 계산하는 방법
데이타 추가시 날짜형태를 함수로 처리 date_add(now(),interval +2 month) ->2개월후 date_add(now(),interval +2 day) ->2일후 date_add('2015-12-29 10:10:10',interval +2 day) ->2일후
서브쿼리 내용호출할때 사용 댓글수 많은것 순으로 정렬
select *,( select count(*) from table2 where 1 and table1.idx.code=table2.sidx_code //상위에 해당하는내용과 서브에 해당하는 내용을 일치시켜서 갯수호출함 group by sidx_code ) as count //count로 카운트 갯수를 표기함 from table1 where 1 order by count //전체 적인 지리문을 정렬함 첫번째 테이블 table1 두번째 테이블 table2 table1.idx_code값은 table2.sidx_code값과 동일함 sidx_code값은 여러게 존제함
임으로 정렬하기 order by field ( 컬럼명, "순서1", "순서2",......
정렬하는 방법은 여러방법이 있다 그중에 유용한 정렬방법이다 select * from tablename where idx_code in (3,4,1) order by field (idx_code, 1, 4, 3); idx_code 내용을 1,4,3 순으로 정렬하고자 할때 사용하면 편하다
의견 0
로그인하면 의견을 남길 수 있습니다.
아직 의견이 없습니다. 첫 의견을 남겨보세요.