Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL PHP查询返回指定的结果,而不是所需的所有行_Php_Mysql_Sql - Fatal编程技术网

MySQL PHP查询返回指定的结果,而不是所需的所有行

MySQL PHP查询返回指定的结果,而不是所需的所有行,php,mysql,sql,Php,Mysql,Sql,我有3个主要的表格来获取有关药物的数据 第一个表是药物列表t3: t3包含带名称的药物ID。其他信息现在不适用 现在请专注于med_id=16 表med_pharmacyt1是如果我们的库存中存在药物,则会出现在其中的位置。目前,我只有2只med_id=16股票: 第三张咨询表是我可以从中收集关于该药物服用量的数据,但这里的t1.med\u pharmacy\u id是其中的FK: 现在,在我的PHP页面中,我有下表: <tr class="bg-info" id="af

我有3个主要的表格来获取有关药物的数据

第一个表是药物列表
t3

t3
包含带名称的药物ID。其他信息现在不适用

现在请专注于
med_id=16

表med_pharmacy
t1
是如果我们的库存中存在药物,则会出现在其中的位置。目前,我只有2只
med_id=16
股票:

第三张咨询表是我可以从中收集关于该药物服用量的数据,但这里的
t1.med\u pharmacy\u id
是其中的FK:

现在,在我的PHP页面中,我有下表:

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

问题是,您同时从3个表中进行选择,这将阻止您看到至少一次未给药的药物。。。相反,从诊所中选择所有股票,并加入交易

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
我不是百分之百支持这个,但这应该有帮助。。。我们所做的是根据clinic_id从T1中选择all。然后,如果其他表中存在数据,我们将其连接起来

为了更好地解释这个查询,我首先从主查询中取出了两个额外的表。这样我们就只剩下诊所的初始库存了。使用
左连接
是向主查询“添加额外信息”的正确方法。这意味着,即使其他表中没有信息,相关行也不会被忽略,它在相应的字段中只有
NULL
(这就是为什么我添加了
IFNULL
语句,表示0)。
LEFT JOIN
需要两个主要信息,即应该连接的表以及如何确定哪一行

这里我们使用
使用
关键字进行连接。这很有用,因为两个表共享相同的列名。这通常会告诉MySQL根据第二个表中的列的值来连接这些行。如果表不共享列名,或者所需的操作不是纯equals,我们可以使用ON。在此之后,我们应该像在WHERE子句中那样简单地编写条件


我建议您在最喜爱的数据库手册中阅读更多关于连接的内容。

这看起来很像您的另一个问题?!?如果它真的在探索问题的其他方面,请看我的想法,因为如果表2中没有行,他就不能正确求和?我得到了这个错误
语法错误或访问冲突:1064您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,以了解要使用的正确语法。med_pharmacy_id,clinic_id)LEFT JOIN Medicing t3使用(t1.med_pharmacy_id,c'在第23行的c:\wamp64\www\ncd\php\getMedStat.php中的第12行
您确实复制粘贴错误,在我的代码中没有“.”。您使用的查询与我建议的不同,为什么要添加“t1”?因为第一个错误是没有名为med_pharmacy_id的列。我的错误……我肯定是把标签弄错了les,因为你首先写了第三条:)试试这个编辑?现在它可以正常工作了。现在你可以编辑你的答案并解释这个问题了,因为那里有一些新东西给我。
"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";
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