简体   繁体   中英

return records from 53 tables mysql database with p_id and t_id?

I have 53 tables in mysql database wherein all the tables have same pid (Patient ID) => primary key field in Patients table. when user save test information it may be save with its other attributes alongwith pid. My Problem is that i am trying to find out the way that when the printer users will try to print the tests results for that patient, how i would use sql query to show him that patient tests result. One other thing, i am also saving t_id => test id in each table where t_id is the primary key field from tests table and also with current date (YYYY-mm-dd). Any idea?

You have a challenging problem here, because the result sets of SQL queries are inherently rectangles of data. That is, each row in a result set has the same columns as every other row. However, medical tests don't work like that. Some have several numbers. Others have some textual information. Others contain a very dense trace of data (EKG), or perhaps an image (x-ray). SQL is the Procrustean Bed of IT. It wants everything to be the same.

If you must use a single query of pure SQL to generate this report, your first step in this design is to attempt to imagine how each different test result can be coerced (formatted) to fit in a standardized row.

Once you have done that you then can write SQL code to do that formatting for each test table. This SQL code will probably contain quite a few calls to the CONCAT() or CONCAT_WS() functions. For example, you may have something like this in one of those queries.

CONCAT('HDL: ','ch.hdl,' LDL: ', ch.ldl) AS result

(@Strawberry is going to call me kafir for suggesting you use these functions.)

Then, finally, you might consider taking all these functions and put them together into a 57-way UNION ALL query, wrapped in a query that ORDERs BY the appropriate value, probably test timestamp.

You can do this, but your work product will be a gigantic query. It will be hard to maintain. "Hairball" is the best word for it.

You probably should consider creating your report in your PHP code instead. Start with a query that returns a simple result set like this to give a list of tests in the correct order.

 p_id, t_id, test_type, timestamp

Then loop over this result set and query each separate test table. Write a PHP function (or maybe a PHP class) to handle each kind of test. Format your report with PHP code.

And above all, be patient. It's a good idea to do some hard thinking about the best way for your application to structure this data. This kind of work is difficult and time consuming to get right. There are many ways to achieve good results, but none of them is magic.

I have solved my problem with following code:

.
.
.
<?php 
$p_sno = addslashes($_POST['p_sno']);
$date = addslashes($_POST['date']);
//print_r($_POST);
$counter = 0;
$sqlTables = "SELECT DISTINCT table_name FROM information_schema.columns WHERE column_name ='p_sno'";
foreach($crud->getRecordSet($sqlTables) as $tables){
//echo($tables['table_name'].'<br />');
$tbl = $tables['table_name'];
$sqlPickRecord = "SELECT sno,date_time,t_sno FROM $tbl WHERE p_sno = $p_sno AND STR_TO_DATE(date_time,'%Y-%m-%d') = '".$date."'";
foreach($crud->getRecordSet($sqlPickRecord) as $row){ $counter +=1; 
$pat_name_sql = "SELECT patient_name FROM patient_info WHERE sno = '".$p_sno."'";
$pat_name = $crud->getValue($pat_name_sql,"patient_name");
$test_name_sql = "SELECT test_name FROM test_info WHERE sno = '".$row['t_sno']."'";
$test_name = $crud->getValue($test_name_sql,"test_name");
?>
    <tr>
        <td> <?php echo($counter); ?> </td>
        <td> <?php echo($p_sno); ?> </td>
        <td> <?php echo($pat_name); ?> </td>
        <td> <?php echo($test_name); ?> </td>
        <td> <?php echo($row['date_time']); ?> </td>
        <td> <a target="_blank" class="print_btn" href="print_<?php echo(str_replace(" ","_",$test_name)); ?>.php?tbl=<?php echo($tbl); ?>&p_sno=<?php echo($p_sno); ?>&t_sno=<?php echo($t_sno);?>&date=<?php echo($row['date_time']);?>" title="Print Test Result"> Print </a> </td>
    </tr>
    <?php } 
}
     }
}//search button 
?>
.
.
.
.

Its work for me now.

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