I have Mysql table:
id | deadline | days
1 | 1423695600 | 0
2 | 1426705199 | 1,2,3
I want: if days filed is 0, change deadline to today's date but keep old hour and minute
I have tried but i dont know how to bulid query
SELECT id IF(p.days != 0, deadline) as deadline, days FROM posts
I think you want to convert from unix time stamps to regular date times for this operation:
select addtime(date(now()), time(from_unixtimestamp(deadline))
You can get back a unix time stamp:
select unix_timestamp(addtime(date(now()), time(from_unixtimestamp(deadline)))
Try Case:
SELECT stock.name,
CASE
WHEN stock.quantity <20 THEN 'Buy urgent'
ELSE 'There is enough'
END
FROM stock
Related
I don't know what is wrong in my MYSQL query:
SELECT * FROM package_customers pc
left join installations ins on ins.package_customer_id = pc.id
WHERE pc.status = 'scheduled'
AND CAST(ins.schedule_date as DATE) >='10-27-2017'
The fields are:
status data type enum
schedule_date data type varchar
In the column schedule_date, the data is like this: 10-27-2017 12AM 12PM
I am trying to find date-wise data.
cast function can work if the source data is in acceptable format.
There are some conditions to validate date and time formats.
Your schedule_date column value does not match them. And hence, cast failed.
Please read documentation on Date and Time Types.
You should think of redesigning the table to include schedule_start and schedule_end columns with datetime data type. MySQL has various date and time functions to work with such data fields.
For time being, your varchar date data can be handled in following way.
mysql> SELECT #dt_string:='10-27-2017 12AM 12PM' AS dt_string
-> , #dtime:=STR_TO_DATE( #dt, '%m-%d-%Y %h%p %h%p' ) AS date_time
-> , DATE( #dtime ) AS my_date;
+-----------------------+---------------------+------------+
| dt_string | date_time | my_date |
+-----------------------+---------------------+------------+
| 10-27-2017 12AM 12PM | 2017-10-27 12:00:00 | 2017-10-27 |
+-----------------------+---------------------+------------+
1 row in set (0.00 sec)
I have a table Aud_Usd with a Time with minute increments (08-09-2017 10:00:00) and a Price (FX conversion rate eg. 0.797). I would like to show in a single table:
Time | Price | Time + 5 minutes | Corresponding price to Time + 5 minutes
Essentially I would just like to compare how the price has moved in that period. I have been stuck on showing the corresponding price. Currently I have
SELECT Time, Price, ADDTIME(Time, '00:01:00') As tplus5, ??
Thanks!
Try this:
SELECT au.Time, au.Price, ADDTIME(au.Time, '00:05:00') As tplus5,
(select price from Aud_Usd where Time = ADDTIME(au.Time, '00:05:00')) as
PricePlus5
from Aud_Usd au
You can use SEC_TO_TIME to achieve this. So your query will be as follows:
SELECT Time, Price, ADDTIME(startTime, SEC_TO_TIME(300)) As tplus5, ....
I'm trying to check with sql if a date range (with start and end date) is in a month (regarding year). Month and year are given variables (GET-variables).
This is my table:
+----+------+-------+-----+
| id | name | start | end |
+----+------+-------+-----+
Because an appointment could be just one date (start and end would be the same date), I have to consider this.
My query
SELECT
id,
name,
start,
end
FROM appointments
WHERE (MONTH(start) = ? AND YEAR(end) = ?) OR (MONTH(start) = ? AND YEAR(end) = ?)
ORDER BY start
The problem with this query is that it just checks the start-month and the end-month not the months between. For example if an appointment is over 5 months, this query fails.
If you want to know if a particular month is in the range, then I would suggest turning the dates into a YYYYMM format. This makes the logic relatively easy to express:
where ?*100 + ? between year(start)*100 + month(start) and
year(end)*100 + month(end)
There's a date (its a varchar(30)) in a format, like this
d.m.y
, where
day = a day without leading zeros
m = a month with leading zeros
y = last two numbers of a year
And a table, that looks like this
id | date | price
1 | 7.04.14 | 10
2 | 8.04.14 | 20
3 | 9.04.14 | 30
And when a query is executed,
SELECT `price` FROM `table` WHERE `date` BETWEEN '7.04.14' AND '9.04.14';
it returns nothing
The thing: I cannot change a date format, and I have to get prices between two dates. Is there an easy way of doing this?
Just parse the dates.
SELECT price
FROM `table`
WHERE STR_TO_DATE(`date`, '%d.%m.%y')
BETWEEN STR_TO_DATE(...) AND STR_TO_DATE(...)
Also, consider taking a look at the manual page for STR_TO_DATE.
But as #juergen d writes, it is far better to use date types.
How do I modify this MySQL query to only count leadIDs from table leads where column 'Date' contains the newest (youngest) date?
SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%'
The problem is that leadID can have multiple instances in table leads. The original query result is "4" because of one duplicate. The correct result is "3".
The date is stored in this format: 2011-10-26 18:23:52. The result should take hours and minutes into consideration when determining the youngest date.
TABLE leads:
leadID | date | change
1 | 2011-10-26 18:23:52 | BAD
1 | 2011-10-26 17:00:00 | OK
2 | 2011-10-26 19:23:52 | OK
3 | 2011-10-26 20:23:52 | OK
4 | 2011-10-26 21:23:52 | OK
5 | 2011-10-26 22:23:52 | BAD
I think this is what you're looking for:
select count(distinct l1.leadId) as accepted from leads l1
left join leads l2
on l1.leadId = l2.leadId and l1.date < l2.date
where l2.date is null and l1.`change` like '%OK%'
You must decide what you mean by newest date: the single latest? yesterday? today?
if yesterday, then add this to your query clause
select * from mytable where date >= date_sub(now(), interval 1 day)
if you are using oracle database you can use max() function to extract newest date from the table, further to check with the table for this newest date :-
SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%'
and date_col = (select max(date_col) from leads)
I am assuming that with newest date your mean is about newest in the table data..
changes :- as per changes in question and as per mentioned in commends ..
I think you want to take newest date among the records having "change" column value like '%OK%' and want to count distinct leadId
please try the following query-
SELECT COUNT(distinct leadID) as accepted FROM leads WHERE change like '%OK%'
and date_col = (select max(date_col) from leads WHERE change like '%OK%')
You can try (in case your date is a int like return by time() function)
$sql = "SELECT COUNT(leadID) as accepted FROM leads WHERE change like '%OK%' ORDER BY Date DESC LIMIT 1"
You will only extract the newest entry.
Edit: This shouldalso works for your date format YYYY-MM-DD hh:mm:ss
Edit 2: Okay, I did not understood your question.
You have a table lead: leadid date
You want to count the number of row for the newset date.
Like another pointed out you can use the MAX operator:
SELECT COUNT(distinct leadid)
FROM LEAD AS l,
( SELECT MAX(Date) mdate FROM Lead ) AS MaxDate
WHERE l.date = MaxDate.mdate
AND l.change like '%OK%'