2012-07-12 2 views
0

나는이 두 가지 질문이 하나의 쿼리에서 조인 두 번째 쿼리와 각 이벤트의 조인MySQL의 이벤트를 얻고

제 질문은 어떻게 하나의 쿼리로 모든 작업을 수행 할 수 있습니까? 내가 예를 들어, 다차원 배열을 반환하고 싶습니다

schema

:

가 여기 내 스키마입니다

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [title] => Title 
      [description] => DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription 
      [segment] => title 
      [thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg 
      [cover] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg 
      [locale] => Locale 
      [address] => Rua Afonso Pena, 22, Tijuca 
      [list] => 0 
      [date] => 2013-10-10 10:10:10 
      [created] => 
      [host_id] => 1 
      [count_joins] => 5 
      [joined] => 0 
      [joins] => Array 
       (
        [0] => Array 
         (
          [id] => 4 
          [first_name] => Giovanna 
          [last_name] => Carneiro 
          [email] => [email protected] 
          [username] => gigi 
          [thumbnail] => 1b6453892473a467d07372d45eb05abc2031647a.jpg 
         ) 

        [1] => Array 
         (
          [id] => 5 
          [first_name] => Júlio 
          [last_name] => César 
          [email] => [email protected] 
          [username] => jujuba 
          [thumbnail] => ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4.jpg 
         ) 

        [2] => Array 
         (
          [id] => 3 
          [first_name] => Claudio 
          [last_name] => Cardozo 
          [email] => [email protected] 
          [username] => cazo 
          [thumbnail] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg 
         ) 

        [3] => Array 
         (
          [id] => 1 
          [first_name] => Claudius 
          [last_name] => Ibn 
          [email] => [email protected] 
          [username] => ibnclaudius 
          [thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg 
         ) 

        [4] => Array 
         (
          [id] => 2 
          [first_name] => Elza 
          [last_name] => Virginia 
          [email] => [email protected] 
          [username] => elzavirginia 
          [thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg 
         ) 

       ) 

     ) 

    [1] => Array 
     (
      [id] => 2 
      [title] => Another Title 
      [description] => Description 
      [segment] => another-title 
      [thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpeg 
      [cover] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpeg 
      [locale] => Locale 
      [address] => Travessa Nestor Vitor, 117, Tijuca 
      [list] => 0 
      [date] => 2013-10-10 10:10:10 
      [created] => 
      [host_id] => 3 
      [count_joins] => 5 
      [joined] => 0 
      [joins] => Array 
       (
        [0] => Array 
         (
          [id] => 1 
          [first_name] => Claudius 
          [last_name] => Ibn 
          [email] => [email protected] 
          [username] => ibnclaudius 
          [thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg 
         ) 

        [1] => Array 
         (
          [id] => 4 
          [first_name] => Giovanna 
          [last_name] => Carneiro 
          [email] => [email protected] 
          [username] => gigi 
          [thumbnail] => 1b6453892473a467d07372d45eb05abc2031647a.jpg 
         ) 

        [2] => Array 
         (
          [id] => 3 
          [first_name] => Claudio 
          [last_name] => Cardozo 
          [email] => [email protected] 
          [username] => cazo 
          [thumbnail] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg 
         ) 

        [3] => Array 
         (
          [id] => 5 
          [first_name] => Júlio 
          [last_name] => César 
          [email] => [email protected] 
          [username] => jujuba 
          [thumbnail] => ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4.jpg 
         ) 

        [4] => Array 
         (
          [id] => 2 
          [first_name] => Elza 
          [last_name] => Virginia 
          [email] => [email protected] 
          [username] => elzavirginia 
          [thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg 
         ) 

       ) 

     ) 

) 

첫 번째 쿼리는 이벤트를 돌아가 두 번째 조인. 가능한 경우 하나만 쿼리하면 모든 것을 반환하고 싶습니다.

+0

많은 정보가 있습니다. 스키마와 일부 데이터를 게시하고 결과를 얻으려는 경우, 더 쉽게 삶을 살 수 있습니다. –

+0

첫 번째 게시물을 편집하여 확인하십시오! –

답변

0

순수 SQL의 문제점 중 하나는 단일 쿼리에서 중첩 된 결과를 만들 수 없다는 것입니다. 모든 필드가있는 납작한 레코드 만 반환 할 수 있습니다. 그런 다음 응용 프로그램 계층을 사용하여 레코드를 중첩 된 행으로 파싱합니다.

linq 또는 Hibernate와 같은 ORM을 사용하는 경우 모든 작업이 백그라운드로 진행됩니다.

다음과 같은 결과를 얻을 수 있습니다.

events.id | events.title | events.locale | users.id | users.first_name | users.last_name

사건의 필드가 각 레코드에 중복되는 10 개의 별개 레코드를 반환합니다. 그러면 응용 프로그램 서버가이를 중첩 된 배열로 구문 분석합니다.

플랫 레코드를 얻은 방법과 설명 한 방법, 응용 프로그램 서버에서 구문 분석 한 방법, 그리고 이벤트에 대한 초기 쿼리를 수행하는 쿼리와 별도 쿼리 각 이벤트에 대해 응용 프로그램 서버에서 다시. 둘 다 작동합니다. 이벤트를 얻고 사용자를 얻거나 모든 데이터를 가져 와서 구문 분석하는 것이 자신의 경우에 더 간단하고 빠른 지 여부에 대한 질문 일뿐입니다.

+0

그게 내가 tought이야! 감사! –