问题
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 jointrades
onexchanges
.id
=trades
.exchange_id
wheretrades
.user_id
= 1 andtrade_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
回答1:
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
来源:https://stackoverflow.com/questions/51919822/wherenotbetween-giving-me-error