mysql PHP比较两个表中的值

时间:2021-03-25 15:35:06

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?

我想比较用户表中的“id”列和成绩表中的“studentid”列。如果登录的用户有一个id 5,那么我需要从成绩表中取出带有“names,exam1,exam2”的studentid 5并以html格式显示。我该怎么做呢?

Please help.

2 个解决方案

#1


0  

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:

要进一步了解联接,请访问以下链接:

#2


0  

Try this..

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

#1


0  

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:

要进一步了解联接,请访问以下链接:

#2


0  

Try this..

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