简体   繁体   English

如何显示第一个表中的记录并匹配第二个表中的记录?

[英]How to display the records from the first table and match the records from the second table?

I have two tables, One is for student details and second is for stud_marks. 我有两个表,一个用于学生详细信息,第二个用于stud_marks。

I am inserting the personal information in student table like 我在学生表中插入个人信息,例如

  s_id  | name   | mobile     | email  
    1   | asdff  |1234567831  |asd@gmail.com
    2   | kjhgg  |1231231231  |mnhg@gmail.com
    <!--many more-->

and I am inserting the student marks like 我在插入学生标记,例如

m_id    |sub_name    |mark       | stu_id    |date_of_added
1       |asdasd      |30         | 1       |2019-06-16 17:31:07
2       |oiuytr      |40         | 1       |2019-06-16 17:31:07
3       |okjhyt      |50         | 2       |2019-06-20 20:12:09
4       |ppoiuj      |60         | 1       |2019-06-22 11:41:34
<!--many more--> 

Now, what query I have to use and display the single records. 现在,我必须使用哪些查询并显示单个记录。 Like I have to display the student 1 record. 就像我必须显示学生1记录。 so My expected output is 所以我的预期输出是

 s_id  | name   | mobile     | email  
   1   | asdff  |1234567831  |asd@gmail.com


m_id    |sub_name    |mark       | stu_id    |date_of_added
1       |asdasd      |30         | 1       |2019-06-16 17:31:07
2       |oiuytr      |40         | 1       |2019-06-16 17:31:07
4       |ppoiuj      |60         | 1       |2019-06-22 11:41:34

I am on the edit page and there I have to display the records like 我在编辑页面上,必须在其中显示类似

name:-asdff
mobile:-1234567831
Email:-asd@gmail.com

<!--subject details-->
Sub_name Mark
asdasd   30         
oiuytr   40
ppoiuj   60      

I can display the records on the HTML page. 我可以在HTML页面上显示记录。 Personal information is display. 显示个人信息。 I can use for each for displaying the subject details. 我可以用于显示主题详细信息。

I am using CodeIgniter. 我正在使用CodeIgniter。

I tried below query but I am getting 3 records. 我在下面的查询中尝试过,但得到3条记录。

SELECT * FROM `student` LEFT JOIN stud_marks ON student.s_id=stud_marks.stu_id WHERE student.s_id=1

the output of the query is 查询的输出是

1 asdff 1234567831 asd@gmail.com 1 asdasd 30 1 2019-06-16 17:31:07

1 asdff 1234567831 asd@gmail.com 2 asdasd 40 1 2019-06-16 17:31:07

1 asdff 1234567831 asd@gmail.com 4 asdasd 60 1 2019-06-22 11:41:34

Would you help me out with this issue? 您能帮我解决这个问题吗?

Try this : 尝试这个 :

//assuming your database connection will be in $con
$sql="SELECT * FROM `student` LEFT JOIN stud_marks ON   student.s_id=stud_marks.stu_id WHERE student.s_id=1";
 $result=mysqli_query($con,$sql);
 while($row = $result->fetch_assoc()){
 $name=$row['name'];
 $mobile=$row['mobile'];
 $email=$row['email'];
 $marks[]=$row['mark'];
 $sub_names[]=$row['sub_name'];
 }
 $records=count($sub_names);
 echo 'Name :'.$name; echo '<br>';
 echo 'Mobile :'.$mobile; echo '<br>';
 echo 'Email :'.$email; echo '<br>';
 echo '------'; echo '<br>';
 echo 'Subject    Marks'; echo '<br>';
 for ($x = 0; $x <= $records; $x++) {
 echo "$sub_names[$x]  $marks[$x]   <br>";
 }
 echo '------'; echo '<br>';

CodeIgniter example to extract data: 提取数据的CodeIgniter示例:

$query = $this->db->query("YOUR QUERY");

foreach ($query->result_array() as $row)
{
        $name=$row['name'];
        $mobile=$row['mobile'];
        $email=$row['email'];
        $marks[]=$row['marks'];
        $sub_names[]=$row['sub_names'];
}

For displaying the result, no change in code starting from $records=count($sub_names) in my previous example. 为了显示结果,在我之前的示例中,从$records=count($sub_names)开始的代码没有变化。

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

相关问题 如何显示SQL表中的所有记录并将这些记录与另一个表匹配? - How do I display all records from a SQL table and match those records to another table? 如何使用RedBeanPHP显示表中的所有记录? - How to display all records from a table with RedBeanPHP? 如果第二个表中至少有4条记录,则从两个表中选择记录 - Select records from two tables if atleast 4 records in second table PHP根据MySql第二个表中字符串中包含的值的匹配从一个表中选择记录 - PHP to select records from one table based on match of value contained in string in second table in MySql 仅使用 mysql 查询从第二个表中以第一个表作为数组获取记录 - Getting records from second table with first table as array using mysql query only 从magento的表中获取前20条记录 - Get the first 20 records from the table in magento 从mysql显示记录/表。 问题 - Display records/table from mysql. Issue 显示右表中的最新记录,如果记录不可用,则至少显示左表中的记录 - Display the latest records from the right table and if records not available then at least show the records from left table PHP显示表中的交替记录 - PHP Display Alternating Records from Table 使用if语句显示表中的记录 - display records from a table using if statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM