zoukankan      html  css  js  c++  java
  • Mysql prepare 语法

    最近一直使用语句,SELECT auction_id, auction_name,SUM(new_cart),SUM(new_collect),SUM(total_cart),SUM(total_collect)  FROM tableName WHERE seller_id = ? AND thedate >= ? AND thedate <= ?  GROUP BY auction_id LIMIT ?, ? 不知道是什么意思,原来是Mysql的prepare的应用,防止脚本注入实用的,下面记录一个事例:

    1、set @session_uid='120189386';set @day1='2013-10-10';set @day2='2013-10-10';set @offset='0'; set @limit='10';

    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)

    2、prepare s1 from 'SELECT auction_id, auction_name,SUM(new_cart),SUM(new_collect),SUM(total_cart),SUM(total_collect)  FROM rpt_fmp_eleven_auction_info_d_01 WHERE seller_id = ? AND thedate >= ? AND thedate <= ?  GROUP BY auction_id LIMIT ?, ?';

    Query OK, 0 rows affected (0.01 sec)
    Statement prepared

    3、execute s1 using @session_uid,@day1,@day2,@offset,@limit;

    +------------+--------------+---------------+------------------+-----------------+--------------------+
    | auction_id | auction_name | SUM(new_cart) | SUM(new_collect) | SUM(total_cart) | SUM(total_collect) |
    +------------+--------------+---------------+------------------+-----------------+--------------------+
    |        123 | ??           |             1 |                2 |               3 |                  4 |
    |       1234 | ??           |             1 |                2 |               3 |                  4 |
    +------------+--------------+---------------+------------------+-----------------+--------------------+
    2 rows in set (0.00 sec)

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    特别提醒:prepare对IN支持的不好,所以遇到这样的情况只能自己写程序。比如:

    mysql> select id,auction_id from rpt_fmp_eleven_auction_info_d_00 where auction_id in (111110,12);
    +----+------------+
    | id | auction_id |
    +----+------------+
    |  2 |         12 |
    |  1 |     111110 |
    |  1 |     111110 |
    |  1 |     111110 |
    +----+------------+
    4 rows in set (0.00 sec)

    记住有4条记录,奇迹是

    mysql> set @auctions='111110,12';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> prepare s1 from 'select id,auction_id from rpt_fmp_eleven_auction_info_d_00 where auction_id in(?)';
    Query OK, 0 rows affected (0.00 sec)
    Statement prepared
    mysql> execute s1 using @auctions;
    +----+------------+
    | id | auction_id |
    +----+------------+
    |  1 |     111110 |
    |  1 |     111110 |
    |  1 |     111110 |
    +----+------------+
    3 rows in set (0.00 sec)

    只有3条了,说明只有set @aucitons='111110,12'的第一条记录'111110'生效了~

  • 相关阅读:
    突破极验验证的验证码
    c# 自定义多选下拉列表2
    c#多选下拉框(ComboBox)
    图片缩放+拖动(html)
    使用天天模拟器开发android应用
    FineUI开源版之TreeGrid(修改)
    FineUI开源版之TreeGrid实现
    c# 窗体最小化后截图实现
    c#一个简单的实例告诉你,多继承还可以这么来
    设置控件Enable=false,控件颜色不变
  • 原文地址:https://www.cnblogs.com/liqiu/p/3369797.html
Copyright © 2011-2022 走看看