简体   繁体   中英

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)

this is my sql statement :

    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 :

在此处输入图片说明

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

$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?

If so, you will want to implement a sub-select in your Where clause to grab the 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.

Your subquery should be:

(SELECT MAX(a.starttime) FROM support a 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! Thank you alberto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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