添加搜索之类的查询时,查询结果发生了变化

时间:2017-07-14 11:39:47

标签: php jquery mysql sql pdo

我有这个SQL查询,它显示了正确的结果:

    select t1.med_id,
    t3.med_name,
    t1.med_expiry, 
    t1.med_barcode, 
    t1.med_tablet, 
    t1.med_pill, 
    t1.med_received,
    sum(t2.given_quantity)
    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='361'
    group by 
    t1.med_id,
    t3.med_name,
    t1.med_expiry, 
    t1.med_barcode, 
    t1.med_tablet, 
    t1.med_pill, 
    t1.med_received

我稍微改为:

select t1.med_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 AND t1.med_id LIKE :searchTxt OR t3.med_name LIKE :searchTxt OR t1.med_barcode LIKE :searchTxt OR t1.med_expiry LIKE :searchTxt GROUP BY t1.med_id,t3.med_name, med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received 

此查询位于PHP文件中,我可以根据文本框中输入的内容进行搜索:

$searchTxt = '%'.$_POST['searchTxt'].'%';

$getRes = "select t1.med_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 AND t1.med_id LIKE :searchTxt OR t3.med_name LIKE :searchTxt OR t1.med_barcode LIKE :searchTxt OR t1.med_expiry LIKE :searchTxt GROUP BY t1.med_id,t3.med_name, med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received";
$execGetRes = $conn->prepare($getRes);
$execGetRes->bindValue(':cid', $clinic_id);
$execGetRes->bindValue(':searchTxt', $searchTxt);
$execGetRes->execute();

$fetchRes = $execGetRes->fetchAll();

这是我的jQuery脚本:

var searchFunction = function(){
    var searchTxt = $("#searchTxt").val();
    searchTxt = $.trim(searchTxt);
    //console.log(searchTxt);
    $.ajax({
        url: '../php/searchMedStat.php',
        type: 'POST',
        data: {searchTxt: searchTxt},
        dataType: 'JSON',

        success:function(resp)
        {
            //append data
            $("#med_table tr").fadeOut(400);
            $("#after_tr").before("<tr class='bg-info'><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>");
            $.each( resp, function(key, result)
            {
                //var pid = result['patient_id'];
                //var profileBtn = "<a id='profileBtn'><span class='badge badge badge-info' style='background-color: #0090ff'>Patient Profile</span></a>"
                $("#after_tr").after("<tr id="+result['med_id']+"><td>"+result['med_id']+"</td><td>"+result['med_name']+"</td><td>"
                    +result['med_expiry']+"</td><td>"+result['med_barcode']+"</td><td>"
                    +result['med_tablet']+"</td><td>"+result['med_pill']+"</td><td>"+result['med_received']+"</td><td>");
            });
        },
        error:function(resp)
        {
            console.log(resp);
        }
    });
}
$(document).ready(function()
{
    $("#searchTxt").on('keyup', searchFunction);
    $("#searchBtn").on('click', searchFunction);
});

当我输入我数据库中确实存在的任何内容时,我没有得到任何结果。

2 个答案:

答案 0 :(得分:2)

学习编写SQL,使其具有可读性,可维护性和正确性。两个重要提示:

  • 使用有意义的表别名而不是任意的别名。
  • 从不FROM子句中使用逗号。 始终使用正确的JOIN语法。

当然,请遵循SQL语法。所以,我认为你想要的是这样的东西:

SELECT mp.med_id, m.med_name, mp.med_expiry, mp.med_barcode, 
       mp.med_tablet, mp.med_pill, mp.med_received,
       sum(cm.given_quantity) as given_pills,
       (mp.med_tablet - 
        sum(cm.given_quantity) * mp.med_tablet) / mp.med_pill
       ) as still_tablets,
       (mp.med_pill - sum(cm.given_quantity)) as still_pills
FROM med_pharmacy mp JOIN
     consultation_med cm
     ON JOIN
     medication m
     ON mp.med_pharmacy_id = cm.med_pharmacy_id AND
       mp.med_idm= m.med_id
WHERE mp.clinic_id = :cid AND
      (mp.med_id LIKE :searchTxt OR
       m.med_name LIKE :searchTxt OR
       mp.med_barcode LIKE :searchTxt OR 
       mp.med_expiry LIKE :searchTxt
      )
GROUP BY mp.med_id, m.med_name, med_expiry, mp.med_barcode, mp.med_tablet, mp.med_pill, mp.med_received ;

答案 1 :(得分:1)

这不是答案 - 但您需要更改括号。

select t1.med_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 AND (t1.med_id LIKE :searchTxt OR t3.med_name LIKE :searchTxt OR t1.med_barcode LIKE :searchTxt OR t1.med_expiry LIKE :searchTxt ) GROUP BY t1.med_id,t3.med_name, med_expiry,t1.med_barcode,t1.med_tablet,t1.med_pill,t1.med_received

应该做的伎俩