dongxingji3882 2016-03-20 16:35
浏览 46
已采纳

PHP从mysql中选择数据文本范围[重复]

This question already has an answer here:

I want to select a range from an items table that have a list of item numbers as follows :

id  item_no  qty date
1   1l500bk  15  2015-03-20
2   1l501bu  10  2015-03-20
3   1l1501ye  5  2015-03-21
4   1l1520bu  5  2015-03-21
5   1l1521bk  1  2015-03-22

And I've created a form with two input fields that asks the user to enter From item number & TO item number, as follows :

<input type="text" value="" placeholder="From Item Number" name="item1">
<input type="text" value="" placeholder="To Item Number" name="item2">

Now, When I type FROM 1l1500 - TO 1l1600 .. It should output all values within this range, but its not ! the output shows nothing . However, if I type 1l1500bk to 1l1521bk it will only show 1 output ( 1l1500bk ) ?!

Here is the PHP code after page submission :

$query = "SELECT *,SUM(qty) from s_{$value} WHERE item_no BETWEEN '".$_POST['item1']."' AND '".$_POST['item2']."'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){

    echo "<tr><td align=center width=19%>{$value}</td><td align=center width=19%>{$row['item_no']}</td><td align=center>{$row['qty']}</td><td align=center>{$row['date']}</td></tr>";

}

I hope this makes sense for you guys, I can't seem to find what is the problem ! Thank you


Thanks for @Waleed Ahmed for his help The solution for my question is just by grouping up the id as follows :

$query = "SELECT *,SUM(qty) from s_{$value} WHERE item_no BETWEEN '".$_POST['item1']."' AND  '".$_POST['item2']."'" GROUP BY id;

Plus : If you want to do partial text search

$length = max(strlen($_POST['item1']), strlen($_POST['item2'])); 

$query = "SELECT *,SUM(qty) from s_{$value} WHERE SUBSTRING(item_no, 1, ".$length.") BETWEEN '".$_POST['item1']."' AND  '".$_POST['item2']."'" GROUP BY id;

This output data even if you don't type the whole item number.

</div>
  • 写回答

1条回答 默认 最新

  • drl971115 2016-03-20 16:44
    关注

    According to this query , it will show only one result as an output. To use aggregate function that is (sum(qty)) here, you have to use "Group By" word to show all results. For example

    Not tested, Hope it will work

    $query = "SELECT *,SUM(qty) from s_{$value} WHERE item_no BETWEEN '".$_POST['item1']."' AND '".$_POST['item2']."'" GROUP BY id;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么