简体   繁体   中英

whereNotBetween giving me error

I am trying to write whereNotBetween query but I dont where is my mistake. its not giving me result. I know something is mistake where i am writing near the line whereNotBetween . There is not any other example.

$values = DB::table('exchanges')
            ->leftJoin('trades', 'exchanges.id', '=', 'trades.exchange_id')
            ->select('trades.*')
            ->where('trades.user_id', $user)

           ->whereRaw('TIME(trades.tradedate) NOT BETWEEN exchanges.start_time AND exchanges.close_time')


            ->get();

Unknown column 'trade_date' in 'where clause' (SQL: select TIME(trades.tradedate) AS trade_date from exchanges left join trades on exchanges . id = trades . exchange_id where trades . user_id = 1 and trade_date not between exchanges.start_time and exchanges.start_time)

I want get result if tradedate > $start_time AND $tradedate < $close_time then i want result.

餐桌用品 表交易

this is between range.. yet its giving me result:

 #items: array:69 [▼
0 => {#1106 ▼
  +"id": 3
  +"exchange": "NSE"
  +"created_at": "2018-04-18 13:00:23"
  +"updated_at": "2018-08-14 06:48:24"
  +"deleted_at": null
  +"start_time": "09:15:00"
  +"close_time": "03:30:00"
  +"country_id": null
  +"user_id": 1
  +"symbol_id": 7
  +"exchange_id": 1
  +"market_id": 1
  +"is_action": 1
  +"rate": 13234
  +"tradedate": "2018-06-21 09:20:00"
  +"note": "Kinnari updated"
  +"quantities": 456
  +"stoploss": 6465

You gotta use a Raw query, the one you are using wont work because you are using an alias, the trade_date column does not exist, is an alias... so you'll have to use the subquery directly like: whereRaw('NOT (TIME(trades.tradedate) BETWEEN exchanges.start_time AND exchanges.end_time)')

I assume u have an end_time column... you gotta define a range... for Between... if not, just use directly the IS EQUAL or = operators...

**Edit 2 **

Your ranges are not valid for a BETWEEN comparison.

If the exchange starts at 09:00 AM of one day and ends on 03:00 AM of next day

There will be nothing between 9 and 3, cause the range has to go up. 9AM to 3PM is not a valid range.

For this you'll need a more complex query...

Something like

->whereRaw("NOT
( 
   trades.tradedate 
   BETWEEN 
   (CONCAT(DATE(trades.tradedate), ' ', exchanges.start_time)) 
   AND 
   (CASE 
       WHEN exchanges.close_time > exchanges.start_time 
           THEN (CONCAT(DATE(DATE_ADD(trades.tradedate, INTERVAL 1 DAY)), ' ', exchanges.close_time))
       ELSE (CONCAT(DATE(trades.tradedate), ' ', exchanges.close_time))
   )
)");

What this query does is very simple, it compares if the date is between a valid date, if the period is in the same day, it just appends the same date, if the period is not in the same day, it appends a date forged with date_add 1 day

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM