doulang1945 2019-06-30 10:04
浏览 218

将SQL转换为Laravel

Let's say I have this:

SELECT * FROM drivers where driver_number NOT IN (SELECT driver_number FROM buses) AND station_id = 2 OR driver_number = 'Dr_02'

and it displays this data enter image description here

Then i want to get this data as laravel query and here is what I did:

$drivers = Driver::whereRaw('driver_number not in (select driver_number from buses) AND station_id = ? ', [$station_id].' OR id = ?', [$bus->Driver_id])
                    ->get();

But I get this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'driver_number not in (select driver_number from buses) AND station_id = ?' at line 1 (SQL: select * from drivers where id = 2 driver_number not in (select driver_number from buses) AND station_id = ?)

I think there is error the way put it, that's it gets a type system error. So if anyone knows a better way to do it, you can help.

  • 写回答

3条回答 默认 最新

  • douyong5476 2019-06-30 10:18
    关注

    You can try using

    $task_without_due_date = \DB::select(\DB::raw("SELECT * FROM drivers where driver_number NOT IN (SELECT driver_number FROM buses) AND station_id = '.$station_id.' OR driver_number = '.$bus->Driver_id.'"));
    
    评论

报告相同问题?

悬赏问题

  • ¥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 的时候出了好多问题,遇到这种情况怎么处理?