开发者

Searching using mySql together with PHP

开发者 https://www.devze.com 2022-12-09 05:04 出处:网络
I\'m busy developing an application where users can search within multiple table, now I\'m not sure if I\'m doing it the correct way.

I'm busy developing an application where users can search within multiple table, now I'm not sure if I'm doing it the correct way.

My sample code looks as follows

        SELECT
            s.name,
            s.surname,
            s.id_nr,
            s.student_nr,
            s.createdate,
            s.enddate
        FROM
            Student AS s,
            Student_开发者_C百科Results AS sr
        WHERE
            sr.innovation = "A"
        AND
            s.name = "Test"

Is it the correct way as I do above or should I rather use left joins etc?


At the very least, you need something linking the two tables together - right now you'll basically fetch every student for every 'A' grade, because nowhere in your WHERE clause do you specify that the grade and the student have to match each other.

Instead, you'd need something like this (no idea what your related ID fields would be, but you get the idea...):

SELECT
    s.name,
    s.surname,
    s.id_nr,
    s.student_nr,
    s.createdate,
    s.enddate
FROM
    Student AS s,
    Student_Results AS sr
WHERE
    sr.innovation = "A"
AND
    s.name = "Test"
AND
    s.id_nr = sr.student_nr
0

精彩评论

暂无评论...
验证码 换一张
取 消