I have a problem with a sql query
Please see this screenshot
I want to select data from this two rangs.
Date from 1996-07-04 to 1996-07-08 (rang 1)
and also from 1996-07-15 to 1996-07-18 (rang 2)
this two rangs has multiple data on table but its return null.
Mysql sql query :
SELECT * FROM Orders where OrderDate >= '1996-07-04' and OrderDate <= '1996-07-08' and OrderDate >= '1996-07-15' and OrderDate <= '1996-07-18';
its return null.
I want to select data from this rangs.
It is because of the logical error Try using this:
SELECT * FROM Orders where
(OrderDate >= '1996-07-04' and OrderDate <= '1996-07-08')
or (OrderDate >= '1996-07-15' and OrderDate <= '1996-07-18');
Related
Table : (Employee)
I need to get Data Between two Days I'm using the following query to get data between two dates
SELECT * FROM employee WHERE Price ((DATE('2020-01-01') BETWEEN DATE(start_date) AND DATE(end_date)) OR (DATE('2020-01-03') BETWEEN DATE(start_date) AND DATE(end_date)) OR (DATE(start_date) >= DATE('2020-07-03')) AND DATE(end_date) <= DATE('2020-07-03'));
I Got the following Result :
But I Expect the Following result
So, How can i get my expected result data in mysql
Your query seems to have nothing to do with your sample data.
Based on your data, you seem to need to generate the dates. You can do this using a recursive CTE and then join:
with recursive dates as (
select date('2020-01-01') as dte
union all
select dte + interval 1 day
from dates
where dte < date('2020-01-04')
)
select d.dte, e.end_date, e.employeename
from employee e join
dates d
on d.dte between e.start_date and e.end_date
Here is a db<>fiddle.
I want to display the records using two different date. I tried using the Between
select * from billing where select_client = '2' and order_date BETWEEN '01/06/2018' and '30/06/2018' order by id ASC
It returns JULY month records also. I tried used >= and <=. That query also returns same record.
select * from billing where select_client = '2' and order_date >= '01/06/2018' and order_date <= '30/06/2018' order by id ASC
Kindly help me out to get only the records between the two dates. Thanks in advance
You have to convert the strings to a date to compare it:
select * from billing where select_client = '2' and STR_TO_DATE(order_date, '%d/%m/%Y') BETWEEN STR_TO_DATE('01/06/2018','%d/%m/%Y') and STR_TO_DATE('30/06/2018','%d/%m/%Y') order by id ASC
wrong date format. if order_date is mysql DATE then format should be 2018-06-01
TABLE
Table:
Id Date
1 01-10-15
2 01-01-16
3 01-03-16
4 01-06-16
5 01-08-16
Given two dates startdate 01-02-16 and enddate 01-05-16. I need to get the data from the table such that it returns all data between the closest past date from startdate and closest future date from enddate including the two dates. So the result will look like this.
Result:
Id Date
2 01-01-16
3 01-03-16
4 01-06-16
What I am doing
What I am doing now is fetching the whole data and removing from the array results less than closest fromdate and greater than closest enddate
What I want
What I want is to do this in query itself so that I don't have to fetch the whole data from table each time.
If you column's type is date, use union can do it:
(select * from yourtable where `date` <= '2016-01-02' order by `date` desc limit 1)
-- This query will get record which is closest past date from startdate
union
(select * from yourtable where `date` => '2016-01-05' order by `date` asc limit 1)
-- This query will get record which is closest future date from enddate
union
(select * from yourtable where `date` between '2016-01-02' and '2016-01-05')
Demo Here
Imaging your date is in YYYY-mm-dd
## get rows within the dates
SELECT * FROM tab WHERE ymd BETWEEN :start_date AND :end_date
## get one row closest to start date
UNION
SELECT * FROM tab WHERE ymd < :start_date ORDER BY ymd DESC LIMIT 1
## get one row closest to end date
UNION
SELECT * FROM tab WHERE ymd > :end_date ORDER BY ymd LIMIT 1
Try this
Select *
From
dTable
Where
[Date]
Between
(Select
Max(t1.Date)
From
dTable t1
Where
t1.date <startdate) And
(Select
Min(t2.Date)
From
dTable t2
Where
t2.date >enddate)
If Date is String, STR_TO_DATE and DATEDIFF can be used here.
SELECT id, Date
FROM tab
where
STR_TO_DATE(Date, '%d-%m-%y') BETWEEN('2016-02-01')AND('2016-05-01')
or
id = (SELECT id FROM tab
where STR_TO_DATE(Date, '%d-%m-%y') > '2016-05-01'
ORDER BY DATEDIFF(STR_TO_DATE(Date, '%d-%m-%y'), '2016-05-01') Limit 1)
or
id = (SELECT id FROM tab
where STR_TO_DATE(Date, '%d-%m-%y') < '2016-02-01'
ORDER BY DATEDIFF('2016-02-01', STR_TO_DATE(Date, '%d-%m-%y')) Limit 1)
I have a problem about getting all the data's in one column and will add it by user id.
I Retrieve the data's by this query
SELECT uid, date, timein, timeout,TIMEDIFF(timein, '08:00:00') as DATE_DIFF
FROM sample_tbl
WHERE date >= '2015-11-01'
AND date <= '2015-11-15'
AND uid IN (32,61,53,54,62,57,55,58,34,60,63,59)
AND timein > '08:00:00'
The Output
My Problem is How can I add them? for example uid = 32 should be
uid = 32 and DATE_DIFF = "00:11:00"
I also tried Group By uid and SUM() or is it possible?
Ok iv'e read about SUM() function and add this to my query SUM(minute(TIMEDIFF(timein, '08:00:00'))) and Group it by ID
SELECT uid, date, timein, timeout,SUM(minute(TIMEDIFF(timein, '08:00:00'))) as Total_late
FROM sample_tbl
WHERE date >= '2015-11-01'
AND date <= '2015-11-15'
AND uid IN (32,61,53,54,62,57,55,58,34,60,63,59)
AND timein > '08:00:00'
GROUP BY uid
I want to merge three queries and I want to get an aggregate result for these three queries, how can i achieve this in MySQL?
My queries
SELECT
order_table_no,
count(bill_no),
sum(finaldiscount)
from order_master
where `ORDER_TABLE_NO` like 'HOME%'
and order_time >= '2015-01-01'
and order_time <= '2015-01-20'
SELECT
distinct(ORDER_TABLE_NO),
sum(order_quantity*order_item_price) AS order_master_FINALDISCOUNT,
count(bill_no)
FROM `order_master` order_master
WHERE date(order_master.`ORDER_TIME`) >= '2015-01-01'
AND date(order_master.`ORDER_TIME`) <= '2015-01-20'
and order_table_no not like 'HOME%'
and order_table_no not like 'TAKE%'
GROUP BY ORDER_TABLE_NO
SELECT
order_table_no,
count(bill_no),
sum(finaldiscount)
from order_master
where `ORDER_TABLE_NO` like 'TAKE%'
and order_time >= '2015-01-01'
and order_time <= '2015-01-20'
It is a bit weird that your first and last query don't have any group by clauses. This doesn't require any joins - just use the MySQL IF Control Flow Function (http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if):
select
order_table_no,
count(bill_no),
IF(order_table_no like 'TAKE%' or order_table_no like 'HOME%', sum(finaldiscount), sum(order_quantity * order_item_price)) finaldiscount
from
order_master
where
order_time >= '2015-01-01' and
order_time <= '2015-01-20'
group by
order_table_no