【问题标题】:Result of a query changed when added a search like query添加类似查询的搜索时查询的结果发生了变化
【发布时间】:2017-12-19 12:03:41
【问题描述】:

我有这个 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);
});

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

【问题讨论】:

  • 在您的 php 文件中,您需要在 ajax (resp) 上回显结果
  • 即使在 mysql 工作台中我也得到了一个空结果
  • 啊在搜索词中添加一些'%'?
  • 你需要添加 % like CONCAT(:searchTxt, '%') etc.. for all like
  • $clinic_id 定义在哪里?

标签: php jquery mysql sql pdo


【解决方案1】:

学习编写 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 ;

【讨论】:

    【解决方案2】:

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

    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
    

    这应该可以解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-04-01
      • 2014-11-25
      相关资源
      最近更新 更多