简体   繁体   中英

mysql PHP comparing values in two tables

Let's say I have two tables: "user" and "grade"

my "user" table looks like this:

id|username|password|email

my "grade" table looks like this:

studentid|names|exam1|exam2|...

I want to compare "id" column from the user table with "studentid" column from grade table. If the logged in user has an id 5 then I would need to pull out studentid 5 with "names, exam1, exam2" from the grade table and display it in html format. How do I do this?

Please help.

You need to JOIN both tables,

SELECT  a.*, b.*
FROM    user a
        INNER JOIN grade b
            ON a.ID = b.StudentID
WHERE   a.ID = 5

To further gain more knowledge about joins, kindly visit the link below:

Try this..

SELECT  names, 
        exam1, 
        exam2
FROM  grade g
   INNER JOIN user u
      ON u.id = g.studentid
WHERE u.id = 5

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