I am trying to select all orders from this date 2013-12-13. The information is in integer value. The query will run but is not giving me any info.
SELECT FROM_UNIXTIME(modified)
FROM orders
WHERE (modified BETWEEN '2013-12-13 00:00:00' AND '2013-12-14 00:00:00')
SELECT FROM_UNIXTIME(modified)
FROM orders
WHERE modified BETWEEN UNIX_TIMESTAMP('2013-12-13 00:00:00') AND UNIX_TIMESTAMP('2013-12-14 00:00:00')
Related
I am trying to do what seems like a simple query.. I have a query which works fine until I try to add a subquery to the select clause. I am trying to add a column by querying a second table with the dates I get from the first. I don't know if a join might be better. If you look at my first sample it returns every record in my second table instead of using the date range from the outer select statement.
SELECT `sales`.`date` as 'newdate', `sales`.`material`,
`customer_logs`.`name`, `sales`.`billingqty` ,
(select count(*) from iis_logs where datetime > (select
Date_add(date_format(newdate, "%Y-%m-%d 00:00:00"), interval - 1 day))
and datetime < date_format(newdate, '%Y-%m-%d 00:00:00' and url like
CONCAT('%',material,'%') limit 1) as tr
FROM `sales`
JOIN `customer_logs` ON `customer_logs`.`customer_number` =
`sales`.`soldtopt`
WHERE `date` >= '2017-09-01'
AND `date` <= '2017-09-30'
ORDER BY `date` DESC
LIMIT 10;
If I just type the string as a date in like this it returns within a second:
SELECT `sales`.`date` as 'newdate', `sales`.`material`,
`customer_logs`.`name`, `sales`.`billingqty` ,
(select count(*) from iis_logs where datetime > '2017-09-01 00:00:00'
and datetime < '2017-09-03 00:00:00' and url like
CONCAT('%',material,'%') limit 1) as tr
FROM `sales`
JOIN `customer_logs` ON `customer_logs`.`customer_number` =
`sales`.`soldtopt`
WHERE `date` >= '2017-09-01'
AND `date` <= '2017-09-30'
ORDER BY `date` DESC
LIMIT 10;
It is not taking the value of newdate I am trying to get in select statement, instead it is returning every row in iis_logs table...
This looks like a perfect candidate for a join. FWIW, mysql query optimizer typically does better on joins that subqueries anyway.
This is my SQL statement which pulls back all the fills I've had in a certain time frame. Is there a way to get the list to come up and also pull the SUM of all of them?
SELECT customerName, date, gallons
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
ORDER BY CONVERT(DATE, date) ASC
First, I would write your query as:
SELECT customerName, date, gallons
FROM addFill
WHERE date >= '2015-03-03'
ORDER BY date ASC;
I see no value in ordering by the date and not the date/time component. Also, you might as well just use a recognizable date format for the comparison.
If you want the sum as well, then that is tricky. One method uses rollup:
SELECT customerName, date, SUM(gallons) as gallons
FROM addFill
WHERE date >= '2015-03-03'
GROUP BY customerName, date with rollup
sums of fills per customer since '3-3-2015'
SELECT customerName, date, sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
GROUP BY customerName
sum of fills for all customers since '3-3-2015'
SELECT sum(gallons)
FROM addFill
WHERE date >= CONVERT(datetime, '3-3-2015' )
I want to return data that is not between current date and the last 7 days.
My SELECT statement appears ok, but it is also returning the current day's data.
SELECT
customer.id AS id,
customer.customer_id AS customer_id,
customer.name AS name,
customer.phone1 AS phone1,
customer.location_area AS location_area,
sales.post_date AS post_date
FROM
sales
INNER JOIN
customer
ON
sales.customer_id = customer.customer_id
WHERE
post_date
NOT BETWEEN
CAST( DATE_SUB(NOW(), INTERVAL 7 DAY) AS DATE )
AND
CAST( NOW() AS DATE )
ORDER BY
sales.id
DESC
LIMIT 30
Please note the customer_id field used in the ON clause is not a primary key in any of the two referenced tables.
What might be missing in my query?
This problem is usually confusion about the different meanings of DATE datatypes on the one hand and TIMESTAMP or DATETIME data types on the other.
Let's say NOW() is 1-April-2017 09:35. And, let's say you have a row in your sales table with a post_date value of 1-April-2017 08:20. Let's say your post_date column has the data type DATETIME.
Then your WHERE clause looks like this after values are applied.
WHERE '2017-04-01 08:20' NOT BETWEEN CAST( '2017-03-25 09:35' AS DATE )
AND CAST( '2017-04-01 09:35' AS DATE )
Applying the CAST operations, we get.
WHERE '2017-04-01 08:20' NOT BETWEEN '2017-03-25'
AND '2017-04-01'
Finally, when comparing a DATETIME or TIMESTAMP to a DATE value, the DATE value is interpreted as having a time of midnight. So your query looks like this:
WHERE '2017-04-01 08:20' NOT BETWEEN '2017-03-25 00:00:00'
AND '2017-04-01 00:00:00'
And, guess what? '2017-04-01 08:20' is after '2017-04-01 00:00:00'.
What you need is this:
WHERE
NOT (
post_date >= CURDATE() - INTERVAL 7 DAY --on or after midnight 2016-3-25
AND post_date < CURDATE() + INTERVAL 1 DAY --before midnight 2016-04-02
)
Please notice that this expression encompasses eight days total.
You can't use BETWEEN for this kind of comparison because you need < for the end of the range, and BETWEEN uses <= for the ends of all its ranges.
Also, CURDATE() is much easier to read than CAST(NOW() AS DATE).
There is a table in that one date column having so many entry of totalamount at different-different time of same date but I don't worry about time. I need to find certain date all totalamount, along with username.
When I am using below query. It shows syntax error
select username,totalamount,date from bill where date = '2013-04-12'
In the above query date is column name
Please help me.
if you want to find all records on a given DATE when there is DATETIME saved in the db, use this method
SELECT username, totalamount, `date`
FROM bill
WHERE `date` BETWEEN '2013-04-12 00:00:00' AND '2013-04-12 23:59:59'
If you want sum of total amount for each user on a given date
SELECT username, SUM(totalamount)
FROM bill
WHERE `date` BETWEEN '2013-04-12 00:00:00' AND '2013-04-12 23:59:59'
GROUP BY 1
If you want sum of total amount for given date
SELECT DATE(`date`), SUM(totalamount)
FROM bill
WHERE `date` BETWEEN '2013-04-12 00:00:00' AND '2013-04-12 23:59:59'
GROUP BY 1
select username,SUM(amount), DATE_FORMAT(date, '%Y-%m-%d') date_only
from bill
where date = str_to_date('2013-04-12','%Y-%m-%d)
group by DATE_FORMAT(date, '%Y-%m-%d'), username
Try this.
If I have a column in a table called creation_date in the database that contain unix timestamp values, how do I get results returned that were only created between for example May 10th 2013 and June 9th 2013?
I tried the below but all I got returned was 0; which isn't correct.
SELECT COUNT(*) FROM my_table WHERE FROM_UNIXTIME(creation_date) BETWEEN '05-10-2013 00:00:00' AND '06-09-2013 23:59:59';
1) If creation_date is a timestamp stored as TIMESTAMP:
SELECT
COUNT(*)
FROM
my_table
WHERE
creation_date
BETWEEN '2013-05-10 00:00:00'
AND '2013-06-09 23:59:59';
2) If creation_date is a timestamp stored as INT:
SELECT
COUNT(*)
FROM
my_table
WHERE
creation_date
BETWEEN UNIX_TIMESTAMP('2013-05-10 00:00:00')
AND UNIX_TIMESTAMP('2013-06-09 23:59:59');