查询返回空值
问题描述:
我有3张关于用药和库存的表格。第一个表格是简单的id
和med_name
,它被称为medication
。查询返回空值
第二张表是关于每种药物和我们有多少药丸。该表称为med_pharmacy
。
第三张桌是我们给每个med多少钱。它被称为consultation_med
。
现在,我创建了一个AJAX请求,所以当我们想给患者一些药片时,看看我们是否还有或没有添加到数据库之前。如果我们仍然有,我会回应good
,如果不是我回应exceeded
。
这里是表:
我的问题是,在初始化,我的意思是,当我们得到药物的数量每例如药片有一med_id = 16
,我将有一个新增加到表2中,但没有增加到表3,因为我们仍然没有给任何病人任何药物。
因此,当我第一次为每种药物使用以下脚本时,按钮id=add_more
将保持禁用状态,因为查询返回null,并且表3没有至少一条此药物的记录。所以如果我第一次需要给病人20粒药片,我就不能点击按钮,即使我已经有100粒药片了。
我该如何解决这个问题?我应该在表3中添加一个填满0
的空行到given_quantity
的领域,每当一包新药出现时,这个问题就会得到解决?
var calculate = function()
{
var quant = $('#medication_quantity').val();
var med_p_id = $("#medication_id").val();
console.log(med_p_id)
$.ajax({
url: '../php/ensureQuantity.php',
type: 'POST',
data: { quant: quant, mid: med_p_id},
dataType: 'TEXT',
success:function(resp)
{
console.log(resp);
if(resp=="exceed")
{
$("#add_more").prop('disabled', true);
$("#danger_message").show();
}
else
{
$("#add_more").prop('disabled', false);
$("#danger_message").hide();
}
},
error:function(resp)
{
console.log(resp);
}
})
}
有了这个PHP代码:
$cid = $_SESSION['clinic_id'];
$mid = $_POST['mid'];
$quant = $_POST['quant'];
$still=0;
$ensureQuantity = "SELECT
t1.med_pharmacy_id, t1.med_id,
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 AND t1.med_pharmacy_id = :mid)
GROUP BY t1.med_pharmacy_id, t1.med_id,t3.med_name, t1.med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received";
$execEnsureQuantity = $conn->prepare($ensureQuantity);
$execEnsureQuantity->bindValue(':cid', $cid);
$execEnsureQuantity->bindValue(':mid', $mid);
$execEnsureQuantity->execute();
$res = $execEnsureQuantity->fetch();
if($res['still_pills']==null || $res['still_pills']=="")
{
$still = 0;
}
else
{
$still = $res['still_pills'];
}
if($quant>$still)
{
echo "exceed";
}
else
{
echo "good";
}
答
我已经找到了解决办法,如下:
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 and t1.med_pharmacy_id=:mid GROUP BY t1.med_pharmacy_id, t1.med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received
和更改的jQuery脚本:
var calculate = function()
{
var quant = $('#medication_quantity').val();
var med_p_id = $("#medication_id").val();
console.log(med_p_id)
$.ajax({
url: '../php/ensureQuantity.php',
type: 'POST',
data: { quant: quant, mid: med_p_id},
dataType: 'JSON',
success:function(result)
{
var remaining = result['still_pills'];
if(remaining == null)
{
remaining = result['med_pill'];
}
if(quant>parseInt(remaining))
{
//console.log('1* Quant:'+quant+'_'+remaining);
$("#add_more").prop('disabled', true);
$("#danger_message").show();
}
else
{
console.log('2* Quant:'+quant+'_'+remaining);
$("#add_more").prop('disabled', false);
$("#danger_message").hide();
}
// if(resp=="exceed")
// {
// $("#add_more").prop('disabled', true);
// $("#danger_message").show();
// }
// else
// {
// $("#add_more").prop('disabled', false);
// $("#danger_message").hide();
// }
},
error:function(result)
{
console.log(result);
}
})
}
$(document).ready(function()
{
$('#medication_quantity').on('keyup', calculate);
$('#medication_id').on('change', calculate);
})
我想你应该在你的数据库查询中使用左连接(s)。此外,你可能需要使用更多的状态,不仅“超”和“好”。我会建议将JSON对象从PHP返回给jQuery。在这样一个对象中,你可以存储更多的信息,这将允许你区分缺货和从不管理。不要在表中插入不必要的空行 – user1915746
你可以帮助jQuery的一部分。我无法知道如何获取单行并将其发送回jquery – droidnation
或如何使用左连接,所以如果一个表不存在,我们可以显示空值 – droidnation