2017-09-15 8 views
0

多対多の関係を持つ3つのテーブルがあります。私はこの製品の特定のユーザーと特定の製品最後に挿入された行を選択したいと思います。特定の条件のMySQL選択行とそれらの条件の最後に挿入された行が多数から多数

テーブル 'ユーザーは' は、ユーザの個人情報が含まれています

userID  persID  firstName  lastName  gender 
42   9559000 Jane   Rae   female 
43   9559001 John   Doe   male 
. 
. 
. 

テーブル 'ストレージ1は、' 製品情報が含まれています

storageID  product  productAttribute  productSize  storageQTY 
1    shirt  red     S    10 
2    shirt  blue     M    10 
. 
. 
. 
13   shirt  green    L    10 
14   shirt  green    XL    9 
15   shirt  green    XXL    7 
. 
. 
39   trousers male     60    10 
40   trousers male     62    8 
. 
. 
59   shoes  standard    41    10 
60   shoes  standard    42    7 
61   shoes  standard    43    10 
62   shoes  standard    44    10 
63   shoes  standard    45    9 
. 
. 
72   jacket  red     L    10 
73   jacket  red     XL    9 
74   jacket  red     XXL    6 

とテーブルactivityRecords:

recordsID  userID  storageID  startDate  expDate 
99   43   15   2017-09-14 
100   43   74   2017-09-14 
101   43   39   2017-09-14 
102   43   13   2017-09-14 
103   43   14   2017-09-14 
104   43   40   2017-09-14 
105   43   14   2017-09-14 
106   43   63   2017-09-14 
107   43   59   2017-09-14 

これまでのところ私は、特定のユーザーに関連する製品の最大値のみをもたらすこのコードを持つこと。

persID  firstName  lastName  gender  shoes  trousers  shirt  color  jacket  startDate  expDate 
9559001 John   Doe   male  45  null   null  null  null  2017-09-14 
9559001 John   Doe   male  null  62   null  null  null  2017-09-14 
9559001 John   Doe   male  null  null   XXL  green  null  2017-09-14 
9559001 John   Doe   male  null  null   null  null  XXL  2017-09-14 

が、私はこれを取得したい:これを結果

select 
     persID, 
     firstName, 
     lastName, 
     gender, 
     max(case when storage1.product = 'shoes' then storage1.productSize end) shoes, 
     max(case when storage1.product = 'trousers' then storage1.productSize end) trousers, 
     max(case when storage1.product = 'shirt' then storage1.productSize end) shirt, 
     max(case when storage1.product = 'shirt' then storage1.productAttribute end) color, 
     max(case when storage1.product = 'jacket' then storage1.productSize end) jacket, 
     startDate, 
     expDate 
    from activityRecords 
     left join user on activityRecords.userID = user.userID 
     left join storage1 on activityRecords.storageID = storage1.storageID 
    where persID='9559001' 
    group by persID, firstName, lastName, gender, startDate, expDate 
    order by persID 

靴番号41はactivityRecordsテーブルの靴のための最新のエントリ(recordsID 107)であるため、

persID  firstName  lastName  gender  shoes  trousers  shirt  color  jacket  startDate  expDate 
9559001 John   Doe   male  41  null   null  null  null  2017-09-14 
9559001 John   Doe   male  null  62   null  null  null  2017-09-14 
9559001 John   Doe   male  null  null   XL  green  null  2017-09-14 
9559001 John   Doe   male  null  null   null  null  XXL  2017-09-14 

をし、シャツサイズXLはactivityRecordsテーブル(recordsID 105)のシャツの最新エントリです。 現在、靴の選択レコードID 106とシャツセレクトはrecordsID 99です。これは選択したコード(max(大文字と小文字)を使用しているため)と関連していますが、選択方法はわかりません特定の製品と人に対する最新のエントリ。誰もがこれで私を助けることができますか?ありがとうございました

答えて

0

結果をより簡単に得るためにサブクエリまたはビューを続行することをお勧めします。

Views Syntax Reference

次のような各製品のビューを作成できます:あなたは、すべてのユーザーのために、最新のシャツの活動を取得したい場合は

create view shirt_activity_view 
as select 
    ar.userID, 
    ar.recordsID, 
    s1.productSize as shirt, 
    s1.productAttribute as color, 
    ar.startDate, 
    ar.expDate 
from activityRecords as ar 
    inner join storage1 as s1 
    on ar.storageID = s1.storageID 
    and s1.product = 'shirt'; 

を:

select 
    * 
from shirt_activity_view 
inner join (
    select 
     userID, 
     max(startDate) as max_start_date 
    from shirt_activity_view 
    group by userID 
) as max_dates 
    on shirt_activity_view.userID = max_dates.userID 
and shirt_activity_view.startDate = max_dates.max_start_date 

あなたはこの最後のクエリを新しいビューまたはより複雑なクエリのサブクエリとして使用できます。このようにして

、あなたも

+0

(あなたが結果セットに期待しているすべての列を使用して)すべてのデータのための平坦化構造を得ることができることは、複数の最後の行を示し、それは私のために大丈夫です。ありがとう – tomas

関連する問題