简体   繁体   English

SQL Select查询返回不需要的数据

[英]SQL Select query returning unwanted data

I have a table that is populated with data from a query that selects studentid numbers from a database. 我有一个表,其中填充了来自从数据库中选择学生编号的查询中的数据。 the view is like so : 视图是这样的:

图片

As you can see the same piece of data is being output multiple times. 如您所见,同一条数据正在多次输出。 I have a feature that for any reason a counselor can not finish a student then they can send the student to another counselor. 我有一个功能,无论出于任何原因,辅导员都无法完成学生的学习,然后他们可以将学生转交给另一位辅导员。 Now to maintain historic data I keep all that data. 现在,要保留历史数据,我将保留所有这些数据。 Now when I go back to my student que then it shows all the current data (which I want) and all the past historic data (which I dont want) 现在,当我回到学生que时,它将显示所有当前数据(我想要的)和所有过去历史数据(我不需要的)

this is my sql statement : 这是我的sql语句:

    try 
                {
            $query = $dbh->prepare("SELECT 
        session.session_id AS id, 
        session.anum, 
        student.first, 
        student.last, 
        session.why, 
        session.studentcomments, 
        session.aidyear,
        support.counselorcomments 
        FROM 
        student INNER JOIN session ON student.anum = session.anum 
        INNER JOIN session_status ON session.status = session_status.status 
        LEFT JOIN support ON session.session_id = support.session_id
        WHERE session.status = 0 OR session.status = 2");

    $query->execute();
            $result = $query->fetchall();
                }
                    catch (PDOException $e) {
                    error_log($e->getMessage());
                    die($e->getMessage());
}

Now the way I have my database set up is how I want to keep it as it works. 现在,我建立数据库的方式就是如何保持其正常工作。 I just think the way I am querying the data is what the problem is. 我只是认为查询数据的方式就是问题所在。

this is a picture of the data in mysql : 这是mysql中数据的图片:

在此处输入图片说明

EDIT 编辑

Now since that problem is fixed now I am having an error that when ever a insert happens (updating the support.starttime timestamp) the above record disappears this is my updated sql 现在,由于该问题已解决,现在我遇到一个错误,即无论何时发生插入(更新support.starttime时间戳记),上述记录都会消失,这是我更新的sql

$query = $dbh->prepare("SELECT session.session_id AS id, session.anum, student.first, student.last, session.why, 
                        session.studentcomments, session.aidyear, support.counselorcomments FROM student LEFT JOIN 
                        session ON student.anum = session.anum LEFT JOIN session_status ON 
                        session.status = session_status.status LEFT JOIN support ON session.session_id = support.session_id
                        WHERE (session.status) IN (0) OR (session.status) IN (2) 
                        AND support.starttime = (SELECT MAX(support.starttime) FROM support INNER JOIN session ON session.session_id = 
                        support.session_id)");

If I am reading your question correctly, you only want the most current row from the Session table, correct? 如果我正确地阅读了您的问题,那么您只想要Session表中的最新行,对吗?

If so, you will want to implement a sub-select in your Where clause to grab the MAX(session.starttime). 如果是这样,您将希望在Where子句中实现子选择以获取MAX(session.starttime)。

If this is the case, you can use the following syntax: 在这种情况下,可以使用以下语法:

WHERE ...(what you already have)...
  AND session.startime = (SELECT MAX({YourTable}.starttime)
                            FROM {YourTable}
                           WHERE {YourTable}.anum = session.anum)

If I had the details of all the tables involved, I would write the entire query for you. 如果我有涉及的所有表的详细信息,我将为您编写整个查询。 Does this answer your question? 这回答了你的问题了吗?

Update answer: 更新答案:

You must tell the subquery which session_id you want the starttime for, you do this by joining your subquery table with your main query resultset. 您必须通过将子查询表与主查询结果集连接在一起,来告诉子查询您希望启动哪个session_id。

Your subquery should be: 您的子查询应为:

(SELECT MAX(a.starttime) FROM support a WHERE a.session_id = session.session_id) (SELECT MAX(a.starttime)从支持WHERE a.session_id = session.session_id)

SELECT DISTINCT 
    session.session_id as id,
    session.anum,
    student.first,
    student.last,
    session.why,
    session.studentcomments,
    session.aidyear,
    support.counselorcomments
FROM student
        INNER JOIN session 
            ON student.anum = session.anum
        LEFT JOIN support 
            ON session.session_id = support.session_id
        WHERE session.status IN (0,2)

Used DISTINCT as ALberto told me to do! 像ALberto告诉我的那样使用DISTINCT! Thank you alberto 谢谢阿尔贝托

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM