【问题标题】:LARAVEL production.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown columnLARAVEL 生产。错误:SQLSTATE [42S22]:找不到列:1054 未知列
【发布时间】:2020-06-28 15:23:11
【问题描述】:

查询时,我想使用 store_id 而不是 nd_001_store_id。我收到如下错误 production.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'store_id' in 'where clause'

$user_store_id = Session::get('user_store_id'); //for example 6
$a = "2020-06-20 13:00:00";
$b = "2020-07-05 13:00:00";
$sales_types = ['retail', 'wholesale'];
$invoice_items_stock_ids = InvoiceItems::select(
    'nd_001_stock_id AS stock_id',
    'nd_000_store_id AS store_id',
    DB::raw("CONCAT(add_date_store, ' ', add_hours_store) AS date_range")
)->where(function ($query) use ($user_store_id) {
    $query->where('store_id', '=', $user_store_id);
})->orWhere(function ($query) use ($a, $b) {
    $query->where('date_range', '>=', $a)
            ->where('date_range', '<=', $b);
})
->whereIn('type', $sales_types)->get();

date_range 和 store_id 列未找到错误

谢谢, 最好的问候

【问题讨论】:

  • date_range 和 store_id 列在 InvoiceItems 表中吗?
  • 您的 where 查询正在查找 store_id 列。您应该可以直接查询nd_000_store_id-&gt;where('nd_000_store_id', $user_store_id).
  • @OMR 没有。我使用了 'nd_000_store_id AS store_id' 和 DB::raw("CONCAT(add_date_store, ' ', add_hours_store) AS date_range")。表中有nd_000_store_id、add_date_store、add_hours_store列

标签: php mysql sql laravel


【解决方案1】:

请尝试whereRaw

$invoice_items_stock_ids = InvoiceItems::select(
    'nd_001_stock_id AS stock_id',
    'nd_000_store_id AS store_id',
    DB::raw("CONCAT(add_date_store, ' ', add_hours_store) AS date_range")
)->where(function ($query) use ($user_store_id) {
    $query->whereRaw('store_id', '=', $user_store_id);
})->orWhere(function ($query) use ($a, $b) {
    $query->whereRaw('date_range', '>=', $a)
            ->whereRaw('date_range', '<=', $b);
})
->whereIn('type', $sales_types)->get();

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 2015-12-08
    • 2018-11-02
    • 2019-10-02
    • 2017-04-03
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多