MySql 选择资格到期日期

发布于 2024-11-08 12:27:36 字数 767 浏览 0 评论 0原文

我有三个 MySql 表:

Users:UserId、UserName

Tests:TestId、TestName

Passes:PassId、UserId、TestId、DateTaken

我想返回一个表格,显示每个用户通过的最新测试的日期,例如,

|--------|---------|---------|---------|---------|---------|
|User    |Test A   |Test B   |Test C   |Test D   |Test E   |
|--------|---------|---------|---------|---------|---------|
|James   |Null     |6/3/11   |Null     |Null     |4/3/11   |
|Mark    |Null     |1/4/11   |8/5/11   |23/5/10  |Null     |
|--------|---------|---------|---------|---------|---------|

在此示例中,James 从未通过测试 A、C 或 D。他可能已多次参加测试 B,但最近一次是在 2011 年 6 月 3 日。

我计划在 ASP.NET GridView 中显示此数据。最好的方法是什么 - 可以在 SELECT 语句中完成吗?请帮忙!

非常感谢。

I have three MySql tables:

Users: UserId, UserName

Tests: TestId, TestName

Passes: PassId, UserId, TestId, DateTaken

I would like to return a table showing dates of the LATEST tests passed by each user, e.g.

|--------|---------|---------|---------|---------|---------|
|User    |Test A   |Test B   |Test C   |Test D   |Test E   |
|--------|---------|---------|---------|---------|---------|
|James   |Null     |6/3/11   |Null     |Null     |4/3/11   |
|Mark    |Null     |1/4/11   |8/5/11   |23/5/10  |Null     |
|--------|---------|---------|---------|---------|---------|

In this example, James has never passed Tests A, C or D. He may have taken Test B several times, but the latest was on 6/3/11.

I plan to display this data in an ASP.NET GridView. What is the best method for this - can it be done in a SELECT statement? Please help!

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心的憧憬 2024-11-15 12:27:36
select
u.username,
max(case when testid = 1 then datetaken else null end) as A,
max(case when testid = 2 then datetaken else null end) as B,
max(case when testid = 3 then datetaken else null end) as C,
max(case when testid = 4 then datetaken else null end) as D,
max(case when testid = 5 then datetaken else null end) as E
from users as u
left join passes as p on u.userid = p.userid
group by u.userid
select
u.username,
max(case when testid = 1 then datetaken else null end) as A,
max(case when testid = 2 then datetaken else null end) as B,
max(case when testid = 3 then datetaken else null end) as C,
max(case when testid = 4 then datetaken else null end) as D,
max(case when testid = 5 then datetaken else null end) as E
from users as u
left join passes as p on u.userid = p.userid
group by u.userid
暮倦 2024-11-15 12:27:36

为了实现这一点,您需要对每个测试结果使用子查询
试试这个:

SELECT User,
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test A') AS 'Test A',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test B') AS 'Test B',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test C') AS 'Test C',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test D') AS 'Test D',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test E') AS 'Test E'
FROM Users u

To achieve this you would need to use subqueries for each test result
Try this:

SELECT User,
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test A') AS 'Test A',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test B') AS 'Test B',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test C') AS 'Test C',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test D') AS 'Test D',
    (SELECT MAX(DateTaken) FROM Passes p INNER JOIN Tests t ON p.TestId=t.TestId WHERE p.UserId=u.UserId AND TestName='Test E') AS 'Test E'
FROM Users u
GRAY°灰色天空 2024-11-15 12:27:36

这是相当通用的解决方案。 MySQL 确实支持我写的所有内容,但是可能有一个 MySQL 特定的解决方案可能更好。

Select
    UserName,
    Max(TestA) testa,
    Max(TestB) testb,
    Max(TestC) testc,
    Max(TestD) testd,
    Max(TestE) teste
FROM
(



    SELECT
        u.UserName,
        Case When TestName = "Test A" then p.DateTaken END TestA,
        Case When TestName = "Test B" then p.DateTaken END TestB,
        Case When TestName = "Test C" then p.DateTaken END TestC,
        Case When TestName = "Test D" then p.DateTaken END TestD,
        Case When TestName = "Test E" then p.DateTaken END TestE
    FROM
        Users u
        LEFT JOIN tests t
        ON u.UserId = t.userid
        LEFT JOIN (SELECT
                Max(DateTaken) DateTaken,
                userId,
                TestId,
            FROM 
                Passes
            GROUP BY 
                userId,
                TestId) p
        ON t.testId = p.TestId) t
Group BY
    UserName

This is fairly generic solution. MySQL does support everything I wrote, however there may be a MySQL specific solution that may be better.

Select
    UserName,
    Max(TestA) testa,
    Max(TestB) testb,
    Max(TestC) testc,
    Max(TestD) testd,
    Max(TestE) teste
FROM
(



    SELECT
        u.UserName,
        Case When TestName = "Test A" then p.DateTaken END TestA,
        Case When TestName = "Test B" then p.DateTaken END TestB,
        Case When TestName = "Test C" then p.DateTaken END TestC,
        Case When TestName = "Test D" then p.DateTaken END TestD,
        Case When TestName = "Test E" then p.DateTaken END TestE
    FROM
        Users u
        LEFT JOIN tests t
        ON u.UserId = t.userid
        LEFT JOIN (SELECT
                Max(DateTaken) DateTaken,
                userId,
                TestId,
            FROM 
                Passes
            GROUP BY 
                userId,
                TestId) p
        ON t.testId = p.TestId) t
Group BY
    UserName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文