2

我有 4 个表需要从中提取数据。我需要统计有多少人参加了单个活动,并查看是否有用户申请了活动。

这些是我的表设置:

TABLE: users
+----+----------+-------+--------+-------+
| id | username | level | class  | guild |
+----+----------+-------+--------+-------+
| 1  | example1 | 100   | Hunter | blah  |
| 2  | example2 | 105   | Mage   | blah2 |
| 3  | example3 | 102   | Healer | blah  |
+----+----------+-------+--------+-------+
ID is primary

TABLE: event_randoms
+----+----------+-------+--------+----------+----------+
| id | username | level | class  | apped_by | event_id |
+----+----------+-------+--------+----------+----------+
| 1  | random1  |  153  | Hunter |    3     |    3     |
| 2  | random2  |  158  | Healer |    3     |    1     |
| 3  | random3  |  167  | Warrior|    1     |    3     |
+----+----------+-------+--------+----------+----------+
ID is primary
apped_by should be foreign key to users.id
event_id should be foreign key to events.id

TABLE: events
+----+------------+------------+-----------+-----------+-----------+
| id | event_name | event_date | initiator | min_level | max_level |
+----+------------+------------+-----------+-----------+-----------+
| 1  |   event1   |    date1   |     1     |    100    |     120   |
| 2  |   event2   |    date2   |     1     |    121    |     135   |
| 3  |   event3   |    date3   |     1     |    100    |     120   |
| 4  |   event4   |    date4   |     1     |    150    |     200   |
+----+------------+------------+-----------+-----------+-----------+
ID is primary

TABLE: event_apps
+----+----------+--------------+
| id | event_id | applicant_id |
+----+----------+--------------+
| 1  |    3     |      2       |
| 2  |    4     |      2       |
| 3  |    3     |      1       |
| 4  |    1     |      3       |
+----+----------+--------------+
ID is primary
event_id should be foreign key to events.id
applicant_id should be foreign key to users.id

我将是第一个承认我对此很陌生的人。几天前我刚刚学会了如何使用 MySQL。我可以从一张桌子上拿东西,但我不确定如何从多张桌子上拿东西。

这是我试过的 SQL 查询

SELECT DD_events.id, event_id, applicant_id, guild, level, class, DD_users.id
      FROM DD_events, DD_event_apps, DD_users
      WHERE DD_event_apps.event_id = DD_events.id
      AND DD_event_apps.applicant_id = DD_users.id

并试图 print_r 一个数组,但该数组变为空。

所以有几个问题与此有关: 1:我如何计算并显示为一个数字有多少人(用户和随机数)注册了一个事件?例如:事件 3 应该总共有 4 个(2 个用户和 2 个随机数)

2:我如何查看某个特定的人是否已为某项活动签名并根据他们是否已签名显示文本?例如:用户 1 已为事件 3 注册,因此它是“已注册”,但未注册的用户 2 将显示“未注册”

3:我想在 2 个表格中显示谁为特定事件签名的信息,1 个用于用户,另一个用于随机数。例如:事件 3 将在 users 表下有 2 个用户信息(用户名、公会、类、级别),然后在随机表中有 2 个随机用户信息(名称、类、级别、应用此人的用户)。

即使您可以回答 1 部分,我们也感谢您提供任何和所有帮助。

4

2 回答 2

3

我认为这将是您的基本查询:

SELECT
    event.id,
    app.applicant_id,
    usr.guild,
    usr.level,
    usr.class,
    usr.id AS Userid
FROM
    DD_events event
JOIN
    DD_event_apps app
    ON  (event.id = app.event_id)
LEFT JOIN
    DD_users usr
    ON  (app.user_id = usr.id)

您可以对此进行修改以聚合它,如下所示:

SELECT
    event.id,
    COUNT(app.applicant_id) AS ApplicantCount,
    COUNT(DISTINCT usr.guild) AS UniqueGuilds,
    COUNT(DISTINCT usr.level) AS UniqueLevels,
    COUNT(DISTINCT usr.class) AS UniqueClasses,
    COUNT(DISTINCT usr.id) AS UniqueUsers
FROM
    DD_events event
JOIN
    DD_event_apps app
    ON  (event.id = app.event_id)
LEFT JOIN
    DD_users usr
    ON  (app.user_id = usr.id)
GROUP BY
    event.id

我可以为您编写这些脚本,但我认为这为您继续提供了一个很好的起点。当您试图获得您正在寻找的结果时,您会发现 T-SQL 相当简单。希望这可以帮助!

于 2012-12-29T16:19:38.983 回答
0
<?php $query = "SELECT count(*) AS numbuh FROM DD_event_apps WHERE event_id = {$row['id']}";
try
{
    // These two statements run the query against your database table.
    $stmt = $db->prepare($query);
    $stmt->execute();
}
catch(PDOException $ex)
{
    // Note: On a production website, you should not output $ex->getMessage().
    // It may provide an attacker with helpful information about your code. 
    die("Failed to run query: " . $ex->getMessage());
}
echo($query);
// Finally, we can retrieve all of the found rows into an array using fetchAll
$count = $stmt->fetchAll();
echo($count['numbuh']); ?>
于 2012-12-30T14:20:58.307 回答