[완료]SQL query, 뭐가 문제인지 모르겠습니다.

lovethecorners의 이미지

음, 프로그래밍문제는 아니구요, 그래도 프로그래밍하다 막히는 문제이니 여기에 올리겠습니다.
아래와 같은 query를 사용하면 timezone으로 정렬이않되고 clinicID로만 정렬이됩니다. query 자체는 문제가 없어보이는데, 당연히 "group by clinicID"를 빼면 문제가없습니다. 뭐가 문제인지 조언을 주시면 감사하겠습니다.
제가 group by에 대한 이해가 부족한것인지도 모르겠습니다.

select clinicID,timezone from users where priority!='deleted' and rep='kwolfe' group by clinicID order by timezone desc limit 30;

+----------+----------+
| clinicID | timezone |
+----------+----------+
|       58 |       -5 | 
|       61 |       -5 | 
|      126 |       -5 | 
|      229 |       -5 | 
|      395 |       -6 | 
|      543 |       -8 | 
|      556 |       -8 | 
|      604 |       -5 | 
|      710 |       -5 | 
|      781 |       -7 | 
|      829 |       -7 | 
|      872 |       -7 | 
|      886 |       -7 | 
|      892 |       -7 | 
|      899 |       -8 | 
|      949 |       -8 | 
|     1159 |       -6 | 
|     1168 |       -6 | 

academic의 이미지

SQL 문 자체가 문제가 있는 것 같습니다.

"limit 30"이라는 부분을 보아하니 mysql인 듯 한데, mysql이 웬만한 오류는 그냥 적당히 알아서 실행을 시키는 편이라서 오류를 찾는데 더 힘든 구석이 있지요.

group by clinicID

라고 했으면

select 로 불러오는 필드는 clinicID 외에는 SUM, AVG, COUNT, COUNT(*), MAX, MIN 같은 집계 함수를 써야 합니다.

따라서 timezone 필드로 order by를 하고 싶다면 그 필드까지 group by에 넣거나...

select clinicID, timezone from users where priority!='deleted' and rep='kwolfe' group by clinicID, timezone order by timezone desc limit 30;

timezone에 집계 함수를 써야 제대로 된 결과가 나올 겁니다.

select clinicID, max(timezone) from users where priority!='deleted' and rep='kwolfe' group by clinicID order by max(timezone) desc limit 30;

----
academic은 제 고등학교 때 동아리 이름입니다.
academic, 아주 가끔은 저도 이랬으면 좋겠습니다.

lovethecorners의 이미지

예, 에구 깜빡했습니다, mysql입니다. group by는 그렇게 사용해야 하는것이군요. 그런데, 처음 방법은 결과에 변함이없고, 두번째 방법이 원하는 결과가 나오는것 같습니다.

답글 감사드립니다.
==
more than one way to do it
말로만 블로거: http://miniz.tistory.com

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.