0

作成中の何らかの評価アプリケーションがあり、ほぼ完成していますが、かなり複雑な SQL クエリで立ち往生しています。

まず、私が使用しているリンクされたテーブル (MySQL) を次に示します。

table1: USERS (ユーザー一覧)

ユーザー ID | 名前 | 姓 | Eメール


table2: EVENT (イベント一覧)

イベント ID | event_typeID | イベント名 | イベントの説明


table3: EVENT_TYPE (イベントの種類のリスト。それぞれに関与した場合に獲得できるポイント数があります)

event_typeID | number_of_point


table4: USER_EVENT (倍率付きのユーザー ID とイベント ID のリスト)

ユーザー_イベント ID | ユーザー ID | イベント ID | 要素


必要なデータを取得する関数は、次のようになります: function getUsersWithPoints($number_of_points_for_threshold)


必要なことは、名前、姓、電子メール、ポイントの合計、およびイベントの合計ポイント数が $number_of_points_for_threshold 以上であるすべてのユーザーについて、ユーザーが行ったすべてのイベントを取得することですが、データは取得せずに間接的に取得する必要があります。それを別の表に。

前もって感謝します。


4

3 に答える 3

0

First I'm assuming that USER_EVENT has the event_typeID as a column. I can't where event_typeID is referenced anywhere else. Try something like this:

SELECT name, surname, email, sum(number_of_point) 
FROM user, user_event, event_type 
WHERE user.userID = user_event.userID AND user_event.event_typeID = event_type.event_typeID 
GROUP BY user.userID
HAVING sum(number_of_point) > :threshold
ORDER BY name ASC

Make sure you have indexes on user_event.userID and user_event.event_typeID a

于 2012-06-03T20:37:21.087 に答える
0

まったく複雑ではありません。これを使用するだけです:

SELECT name, surname, email, SUM(sum_points) AS total_points
FROM (SELECT userID, (SUM(factor) * number_of_point) AS sum_points
      FROM USER_EVENT AS ue INNER JOIN EVENT AS e ON ue.eventID = e.eventID
      INNER JOIN EVENT_TYPE AS et ON e.event_typeID = et.event_typeID
      GROUP BY userID, event_typeID, number_of_point) AS sub
INNER JOIN USERS AS u ON sub.userID = u.userID
GROUP BY u.userID, name, surname, email
HAVING total_points >= $number_of_points_for_threshold

更新:まず、このクエリでは、ユーザーが持っている各イベントの数 (ユーザーが持っている個別のイベントの数だけでなく) が考慮されます。次に、それをどのように達成すると予想されますか? すべての名前を連結する必要があります (そして個別に)。すべての説明も一緒に) - そのためには別のクエリを使用してください:

SELECT e.event_name, e.event_description, ue.factor, (ue.factor * et.number_of_point) AS sum_points
FROM USER_EVENT AS ue INNER JOIN EVENT AS e ON ue.eventID = e.eventID
INNER JOIN EVENT_TYPE AS et ON e.event_typeID = et.event_typeID
WHERE ue.userId = @userId
于 2012-06-03T21:56:25.340 に答える
0

私が考える問題を解決する 1 つの方法は、各ユーザーのポイントの現在の合計をテーブルに格納することです (ここではユーザー テーブルである可能性があります)。イベントがあるときはいつでも、そのユーザーの合計ポイントを更新する必要があります。

于 2012-06-03T20:23:10.250 に答える