Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've a delivery system with the following mysql SELECT:
SELECT tb_pedido.*,tb_usuarios.nome,tb_usuarios.email,tb_usuarios.cpfcnpj, DATE_FORMAT(data_hora, '%Y-%m-%d') FROM tb_pedido,tb_usuarios where tb_pedido.id_usuario=tb_usuarios.id_usuario and (status=2 OR status=4) and DATE(data_hora) = CURDATE() order by id_pedido desc
It selects the orders form today with a specific status. What I want to do is to add a sub query to return all results from yesterday with status 8.
I guess the following subquery would work but I don't know how to add it to the current query:
DATE_ADD(DATE(data_hora), INTERVAL -1 day) AND status=8
Anyone please?
Perhaps something like:
SELECT tb_pedido.*,tb_usuarios.nome,tb_usuarios.email,tb_usuarios.cpfcnpj, DATE_FORMAT(data_hora, '%Y-%m-%d')
FROM tb_pedido,tb_usuarios
WHERE
(tb_pedido.id_usuario=tb_usuarios.id_usuario
and (status=2 OR status=4)
and DATE(data_hora) = CURDATE())
OR
(DATE(data_hora) = DATEADD(day, -1, convert(date, GETDATE()))
and status=8)
order by id_pedido desc
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to show monthly average of total using SQL and PHP.
For example this table has total=254 in June & total=315 in July. I need to show their average i.e. (254+315)/2= 284.5
What SQL query should I make to print their average using PHP?
One method is:
select sum(total) / count(distinct extract(year_month from date))
from t;
This sums the total column and divides by the number of months. If you need this for the particular two months, then include a where clause:
select sum(total) / count(distinct extract(year_month from date))
from t
where date >= '2020-06-01' and date < '2020-08-01'
If you wanted to count missing months as 0 for the average purposes, then use a constant:
select sum(total) / 2
from t
where date >= '2020-06-01' and date < '2020-08-01'
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a mysql record with the columns start_date and end_date. Now I have a date (as example 2017-08-03) and i want to ask if this date is between the start_date and end_date in my mysql table.
Is this possible?
Use between:
SELECT * FROM events
WHERE '2012-01-18' between start_date AND end_date
BTW: Take care of time part if start and end are datetime types
You can use this:
SELECT * FROM events
WHERE start_date<='2012-01-18'
AND end_date>='2012-01-18'
SELECT *
FROM `yourtable`
WHERE (your_found_date BETWEEN 'start_date' AND 'end_date')
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a website in which admins can post news and updates but every time they add a new one it goes from the first one added downwards any idea how to fix this?
You want to sort on the datetime (or if that doesn't exist you could use the ID, but that is not how "it should be") in descending order instead of ascending.
Example query:
SELECT `title`, `text` FROM `news` ORDER BY `datetime` DESC LIMIT 5
https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to display a database record between two date ranges using PHP-mysql.
I am working on a project in which employees payment record is saved in a table. I want to see what amount has been paid between two dates selected through date-picker.
Try this:
SELECT amount FROM mytable WHERE created_at BETWEEN '2011-12-01' AND '2011-12-07';
Assuming mytable to be the table name, created_at to be the date field and amount to be the amount field.
Hope this helps.
Peace! xD
SELECT amount FROM mytable
WHERE
created_at >= '2011-12-01'
AND
created_at <= '2011-12-07';
If you need the sum of the salary between two dates you can do somethig like:
SELECT sum(salary) as total FROM table_name
WHERE job_date BETWEEN 'dd-mm-yyyy' AND 'dd-mm-yyyy';
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What i want to do is to limit a user to deposit/withdraw money in the account only 5 times a week , and after the 5 times user must wait next week to be able to deposit again.
I have a table named depuser with the following rows : uid(userid), date(date of dep/witdraw), type(deposit/witdraw) and the amount. thanks in advance
This query will count the # of deposits recorded in the last 7 days, based on your table structure:
SELECT COUNT(uid) AS total_deposits FROM depuser
WHERE `type` LIKE 'deposit' AND
DATE(`date`) <= NOW() AND
DATE(`date`) >= DATE_SUB( NOW(), INTERVAL 7 DAY)`
From here you can compare the total_deposits value returned and make logic decisions.