SQL:每天选择项目但删除多个条目

时间:2014-08-08 09:01:54

标签: php sql

我有一个访问日志,记录(注册)用户每个项目的每次点击。通过连接表,我得到一个每个用户的好看法,如下所示:

ID | ProjectTitel |项目发布|点击的时间

使用此代码:

$request = "
    SELECT
        u.id,
        u.nachname,
        u.vorname,
        u.login,
        al.project,
        DATE_FORMAT(al.date, '%d. %b %Y, %H:%i') as date,
        a.titel,
        DATE_FORMAT(a.datum, '%d. %b %Y') as datum
    FROM
        user as u
        JOIN accesslog al ON al.user = u.id
        LEFT JOIN ausschreibungen as a ON al.project = a.id
    WHERE
        al.date >= '$interval_from'
        AND al.date <= '$interval_to'
        AND al.project != 0
        AND u.id = ".(int)$_GET['userid']."
    ORDER BY al.date DESC
        ";

当然,输出每次都会让我每次点击每个项目。

所以,现在对于真正的问题,显示的数据应该减少到每个项目每天只显示一次点击。

我尝试按天分组,当然没有用,按项目,当然也没用。

User Table:
ID | Name | other useless stuff
accesslog Table:
User ID | ProjectID | Time
ausschreibungen Table:
Project ID | Project name | other useless stuff

我需要像今天一样向我展示所有的东西,但要删除多个。

在更改sql语句时是否仍然可以实现这种复杂性,还是需要使用php评估数据?

如果后者是唯一的可能性,那么建议从哪里开始?

度过美好的一天!

解决方案:

$request = "
        SELECT DISTINCT
            u.id,
            u.nachname,
            u.vorname,
            u.login,
            al.project,
            DATE_FORMAT(al.date, '%d. %b %Y') as date,
            a.titel,
            DATE_FORMAT(a.datum, '%d. %b %Y') as datum
        FROM
            user as u
            JOIN accesslog al ON al.user = u.id
            LEFT JOIN ausschreibungen as a ON al.project = a.id
        WHERE
            al.date >= '$interval_from'
            AND al.date <= '$interval_to'
            AND al.project != 0
            AND u.id = ".(int)$_GET['userid']."
        GROUP BY al.project, date

        ORDER BY al.date DESC
            ";

1 个答案:

答案 0 :(得分:1)

请尝试此(未经测试)查询。您需要将各种记录的日期/时间值视为日期(我认为)

$request = "
    SELECT
        u.id,
        u.nachname,
        u.vorname,
        u.login,
        al.project,
        date(al.date)
        a.titel,
        MIN(a.datum)
    FROM
        user as u
        JOIN accesslog al ON al.user = u.id
        LEFT JOIN ausschreibungen as a ON al.project = a.id
    WHERE
        al.date >= '$interval_from'
        AND al.date <= '$interval_to'
        AND al.project != 0
        AND u.id = ".(int)$_GET['userid']."
    GROUP BY
        u.id,
        u.nachname,
        u.vorname,
        u.login,
        al.project,
        date(al.date)
        a.titel
    ORDER BY date(al.date) DESC
        ";