简体   繁体   中英

selecting the latest record from multiple tables in one database (MYSQL PHP)

I have school mysql db with 15 tables (15 class rooms), each table has unified fields/simple data like this: ID, DATE, TIME, CLASSROOM_NO, STUDENTS_ALL, STUDENTS_CURRENT, COMMENTS

approach is to be able to keep updating this data all the time through php form that increment that db, that is fine, it is working fine.

in another php page, i have display.php that shows me only the latest record added, so i can know the latest update done to this classroom, and this is also fine, it is exactly what i need, here is my code:

<?php
$con=mysqli_connect("localhost","classroom","mypsassword","mysqluser");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT id, date, time, classroom_no, students_all,     student_current,comments FROM classroom_1 ORDER BY id DESC LIMIT 1");


while($row = mysqli_fetch_array($result))
 {

 echo "<b>Room:</b> " . $row['classroom_no'] . "<br>";
 echo "<b>All students:</b> " . $row['students_all'] . "<br>";
 echo "<b>Available students:</b> " . $row['students_current'] . "<br>";
 echo "<b>Absent:</b> ";
 echo floatval($row['students_all'])  -  floatval($row['students_current']);
 echo "<br>";
 echo "<b>Updated on:</b> " . $row['date'] ."<br>";
 echo "<b>Update time:</b> " . $row['time']."<br>" ;

  }

mysqli_close($con);

?>

now, i have no problem in achieving and retrieving the latest update from each classroom in one page... what i struggle for is to select the latest record from each table and then display them all accordingly in the same page... so to have a php page say all_classrooms.php

and to show all classrooms together (only the latest record update from each table) in one page, so i am not able to understand how to select the sql query from all tables in the same time and have it limited to the latest record...

my tables are in sequencal orders like this: classroom_1 classroom_2 classrrom_3

please advise

As suggested by Kickstart: I would suggest that you normalize your database design. Put all the classroom data into one table with a column to show what classroom any particular row relates to.

Other (workaround) solution that I would not recoment, would be to create a view . That looks like

CREATE VIEW allData AS
SELECT * from Classroom1
UNION ALL SELECT * from Classroom2
....

and then write select from this view.

It would be best to redesign your database with one table for the classrooms, another for the classes (many rows per classroom), etc.

However if that isn't possible you would need something like this:-

SELECT classroom, id, date, time, classroom_no, students_all, student_current, comments
FROM
(
    SELECT '1' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_1
    UNION
    SELECT '2' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_2
    UNION
    SELECT '3' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_3
    UNION
    SELECT '4' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_4
    UNION
    SELECT '5' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_5
    UNION
    SELECT '6' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_6
    UNION
    SELECT '7' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_7
    UNION
    SELECT '8' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_8
    UNION
    SELECT '9' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_9
    UNION
    SELECT '10' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_10
    UNION
    SELECT '11' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_11
    UNION
    SELECT '12' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_12
    UNION
    SELECT '13' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_13
    UNION
    SELECT '14' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_14
    UNION
    SELECT '15' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_15
) Sub1
INNER JOIN
(
    SELECT classroom, MAX(id) AS maxid
    FROM
    (
        SELECT '1' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_1
        UNION
        SELECT '2' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_2
        UNION
        SELECT '3' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_3
        UNION
        SELECT '4' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_4
        UNION
        SELECT '5' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_5
        UNION
        SELECT '6' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_6
        UNION
        SELECT '7' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_7
        UNION
        SELECT '8' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_8
        UNION
        SELECT '9' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_9
        UNION
        SELECT '10' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_10
        UNION
        SELECT '11' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_11
        UNION
        SELECT '12' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_12
        UNION
        SELECT '13' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_13
        UNION
        SELECT '14' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_14
        UNION
        SELECT '15' AS classroom, id, date, time, classroom_no, students_all, student_current, comments FROM classroom_15
    ) Sub3
    GROUP BY classroom
) Sub2
ON Sub1.classroom = Sub2.classroom AND Sub1.id = Sub2.maxid

Note that I have added a fixed classroom column in the UNION. This might duplicate the value of classroom_no.

It is also concerning that there is a column called students_all. That suggests that you have a column containing a list of the students. If so it would be far better to split that off into another table which could have many rows for each class (one per student in a class)

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