私は薬物に関するデータを得るために3つのメインテーブルを持っています。MySQLのPHPクエリで指定された結果が返され、必要な行がすべて返されない
最初の表は、薬剤リストt3
です:
t3
はそれの名前を持つ薬のIDが含まれています。他の情報は現在対象外です。
今のところmed_id = 16
に集中してください。
テーブルmed_pharmacy t1
は、医薬品が当社の在庫に存在する場合に表示されます。現在、私はそれだけでmed_id = 16
の2つの銘柄いる:
3番目の表は、t2
をconsultation_med私はその薬から与えられているどのくらいのデータを収集することができる場所であるが、ここでは、t1.med_pharmacy_id
はFKでありますそれは:
<tr class="bg-info" id="after_tr">
<th>Med ID</th>
<th>Med Name</th>
<th>Med Expiry</th>
<th>Barcode</th>
<th>received</th>
<th>Pills received</th>
<th>Date Received</th>
<th>Pills distributed</th>
<th>Still (in tablets)</th>
<th>Still (in pills)</th>
</tr>
<?php foreach($fetchRes as $res) { ?>
<tr id="<?php echo $res['med_id'] ?>">
<td><?php echo $res['med_id'] ?></td>
<td><?php echo $res['med_name'] ?></td>
<td><?php echo $res['med_expiry'] ?></td>
<td><?php echo $res['med_barcode'] ?></td>
<td><?php echo $res['med_tablet'] ?></td>
<td><?php echo $res['med_pill'] ?></td>
<td><?php echo $res['med_received'] ?></td>
<td><?php echo $res['given_pills'] ?></td>
<td><?php echo floor($res['still_tablets'])?> (Exact:<?php echo number_format($res['still_tablets'], 2) ?>)</td>
<td><?php echo $res['still_pills'] ?></td>
</tr>
<?php } ?>
:
さて、私のPHPページに私は以下の表を持っています
med_id = 16の2番目のタブレットにはstill_pills = 100
が表示されていますが、このタブレットのバーコードが異なる人には薬が与えられていません。それにのみ使用し、患者に与えられた薬のリストを示しています
ここで私は
$clinic_id = $_SESSION['clinic_id'];
$getRes = "SELECT t1.med_id, t1.med_pharmacy_id,
t3.med_name,
t1.med_expiry,
t1.med_barcode,
t1.med_tablet,
t1.med_pill,
t1.med_received,
sum(t2.given_quantity) as given_pills,
t1.med_tablet - ((sum(t2.given_quantity)*t1.med_tablet)/t1.med_pill) as still_tablets,
(t1.med_pill-sum(t2.given_quantity)) as still_pills
FROM med_pharmacy t1, consultation_med t2, medication t3 WHERE t1.med_pharmacy_id = t2.med_pharmacy_id AND t1.med_id=t3.med_id
AND t1.clinic_id=:cid GROUP BY t1.med_pharmacy_id, t1.med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received";
$execGetRes = $conn->prepare($getRes);
$execGetRes->bindValue(':cid', $clinic_id);
$execGetRes->execute();
これはあなたの他の質問とよく似ています。 ?!?それが本当に問題の他の側面を探っているなら、https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-非常にシンプルなSQLクエリ – Strawberry