Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use exclusive BETWEEN clause in mysql?

Tags:

php

mysql

between

I have a php variable. I want check either it lies between values of two columns of any row of mysql table or not. But I do not want match that variable with exact values of table. i.e, exclusive BETWEEN clause. If anyone know answer then please explain with an example. Thank You. Here is my code.

$var = '15:00:00';


//Mysql Table (datatype of columns are TIME)

          first         last
row1      13:00:00      15:00:00
row2      17:00:00      19:00:00
like image 984
Akshay Vaghasiya Avatar asked Oct 18 '25 18:10

Akshay Vaghasiya


1 Answers

There is no "exclusive BETWEEN" operator in MySQL. The expression:

  a BETWEEN b AND c

is shorthand equivalent to:

  ( a >= b AND a <= c )

We can use different comparison operators if we like:

  ( a > b AND a < c )

If there's a requirement to include BETWEEN to achieve a an equivalent, then we could do

  ( a BETWEEN b AND c AND a NOT IN (b,c) )

but I think the SQL is easier if we avoid using BETWEEN.


To check whether there are any rows that match a value, we can supply a second copy of the same value into a second condition e.g.

  SELECT 1 AS `matched`
    FROM mytable t
   WHERE t.first < ?
     AND t.last  > ? 
   LIMIT 1
like image 173
spencer7593 Avatar answered Oct 20 '25 08:10

spencer7593



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!