drouie2014 2015-04-16 16:33
浏览 13

too long

To get an SQL Statement like this;

SELECT postcode FROM Payment WHERE 
  countries IN ('E92000001', 'L93000001') AND 
  counties IN ('95', 'E10000002') AND 
  gors in ('A', 'B');

My code is currently;

$postcode_extract = PostcodeExtract::all();

foreach ($input as $column => $values) {
            $postcode_extract->whereIn($column, $values);
    }

I need to switch those ANDs to ORs. I think I might have it;

foreach ($input as $column => $values) {
    $postcode_extract->orWhere(function ($query) use ($column, $values) {
                                $query->whereIn($column, $values);
                                });
        }

I do get results.. It's hard to tell without seeing the raw query though. I used toSql() and this is the result;

select * from `201502_postcode` where (`country` in (?, ?)) or (`county` in (?, ?)) or (`gor` in (?, ?))

Why the ?s though? Why not the real data or is that not how toSql works?

-- UPDATE --

Here is my code using toSql;

$sql = $postcode_extract->toSql();

print_r($sql);

I do it after the foreach loop

  • 写回答

1条回答 默认 最新

  • dounai1986 2015-04-16 16:47
    关注

    Laravel uses prepared statements so to get sql query and arguments you need to use not only toSql() method but also getBindings(), so to get full query you could use for example:

    $sql = str_replace(['%', '?'], ['%%', "'%s'"], $postcode_extract->toSql());
    $fullSql = vsprintf($sql, $postcode_extract->getBindings());
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?