2017-07-17 10 views
-1

私は薬物に関するデータを得るために3つのメインテーブルを持っています。MySQLのPHPクエリで指定された結果が返され、必要な行がすべて返されない

最初の表は、薬剤リストt3です:

enter image description here

t3はそれの名前を持つ薬のIDが含まれています。他の情報は現在対象外です。

今のところmed_id = 16に集中してください。

テーブルmed_pharmacy t1は、医薬品が当社の在庫に存在する場合に表示されます。現在、私はそれだけでmed_id = 16の2つの銘柄いる:

enter image description here

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 } ?> 

enter image description here

さて、私のPHPページに私は以下の表を持っています

med_id = 16の2番目のタブレットにはstill_pills = 100が表示されていますが、このタブレットのバーコードが異なる人には薬が与えられていません。それにのみ使用し、患者に与えられた薬のリストを示しています

enter image description here

ここで私は

$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(); 
+0

これはあなたの他の質問とよく似ています。 ?!?それが本当に問題の他の側面を探っているなら、https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-非常にシンプルなSQLクエリ – Strawberry

答えて

1

を使用していQUERYは、問題は、あなたがしていることです同時に3つのテーブルから選択すると、1回以上与えられていない場合に薬を見ることができなくなります...代わりに、診療所からすべての株式を選択して取引に参加します。

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 - ((ifnull(sum(t2.given_quantity),0)*t1.med_tablet)/t1.med_pill) as still_tablets, 
(t1.med_pill-sum(t2.given_quantity)) as still_pills 
FROM med_pharmacy t1 
LEFT JOIN consultation_med t2 USING (med_pharmacy_id,clinic_id) 
LEFT JOIN medication t3 USING (med_id,clinic_id) 
WHERE 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 

私はこの上の100%ではないけど、これは役立つはず...我々は何をすべきかclinic_idによるT1からすべての選択です。次に、他のテーブルが存在する場合は、それを結合します。

クエリをより詳しく説明するために、私は最初にメインクエリから2つの余分なテーブルを取り出しました。それが私たちのクリニックのための最初の株式だけで私たちを残すだろう。 LEFT JOINを使用するのは、メインクエリに「追加情報を追加する」適切な方法です。つまり、他のテーブルに情報がなくても、関連する行は無視されません。対応するフィールドにNULLがあるだけです(このため、IFNULLステートメントを追加して0を意味します)。 LEFT JOINには、2つの主要な情報、結合すべきテーブル、およびどの行の決定方法が必要です。

ここでは、結合にUSINGキーワードを使用しています。これは、2つのテーブルが同じ列名を共有するので便利です。これは、通常、MySQLに、これらの列の値に従って2番目のテーブルから行を結合するように指示します。テーブルが列名を共有していないか、または必要な操作が純粋な等価でない場合は、代わりにONを使用できます。その後、WHERE句のように条件を記述するだけです。

お気に入りのデータベースマニュアルでJOINSの詳細を読むことをお勧めします。

+0

このエラーが発生しました。 '構文エラーまたはアクセス違反:1064 SQL構文にエラーがあります。正しい構文についてはMySQLサーバのバージョンに対応したマニュアルをチェックし、 '.med_pharmacy_id、clinic_id'の近くで使用してください)LEFT JOIN medication t3 USING(t1.med_pharmacy_id、C ':\ wamp64 \ www \ ncd \ php \ getMedStat.php on line 23' – droidnation

+0

あなたは貼り付けられたコピーを間違ってコピーしていますが、その場所のコードに '。'はありません。私が提案したのと同じクエリを使用していません。なぜ 't1'を追加しましたか? – Salketer

+0

最初のエラーは、med_pharmacy_idと呼ばれる列がないことでした。 – droidnation

0
"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 limit 1"; 
+0

私はテーブル2に行がない場合、彼は合計権利を行うことができないと思う? – droidnation

関連する問題