Select rows with certain combination - mysql

First of all let me give you guys the SQLFiddle: http://sqlfiddle.com/#!2/1d8bd/11
I've been working on this for a while and am facing some programmers (and writers) block.
This is a sample pseudo code of what I am trying to accomplish in MySQL:
Return all rows WHERE {
(`to_user_id` = '27' AND `to_delete` >= SUBDATE(NOW(), INTERVAL 720 HOUR) )
AND
(`from_user_id` = '27' AND `from_delete` >= SUBDATE(NOW(), INTERVAL 720 HOUR) )
}

Are you looking for this (using OR instead of AND between groups of conditions)?
SELECT *
FROM messages
WHERE (to_user_id = 27 AND `to_delete` >= NOW() - INTERVAL 720 HOUR)
OR (from_user_id = 27 AND `from_delete` >= NOW() - INTERVAL 720 HOUR)
Here is SQLFiddle demo

One issue you're having is your to_delete and from_delete fields are both NULL in your Fiddle example. Therefore, they can never meet your WHERE criteria.
That apart, no need for parentheses if all of your operators are AND. Perhaps you're looking for OR instead?

Related

Records between NOW() and 3am today or yesterday

How can I get records in a table between NOW() and the previous 3am?
This would be easy if it's 9am, but how do I write this if it's 2am? i.e I want the trades between DATE_SUB(CURDATE(), INTERVAL 21 HOURS) and NOW(). I'm looking for some code which can do both without needing to check the time in usercode and choose between two sql statements.
I'm sure there's a simple solution to this, but it's eluding me.
A simple idea is to subtract three hours and compare the date:
where date(date_sub(col, interval 3 hour)) = (case when hour(date) >= 3 then curdate() else date_sub(curdate(), interval 1 day)
Or, more explicitly, just do the comparison in SQL:
where (hour(date) >= 3 and date(col) = curdate()) or
(hour(date) < 3 and date(col) = date_sub(curdate(), interval 1 day)
I'll add my own answer (I finally gave up and explored using IF and found it could be used inside SELECT statements), but #Gordon Linoff might be better - I've no idea which of these is faster.
SELECT IF (NOW() > DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_SUB(CURDATE(), INTERVAL 21 HOUR))
which can then be used as the conditional on an outer SELECT.

Select exact expiry date

I have this record in expiry_date column:
2015-04-30 04:15:29
2015-04-22 06:02:07
I need to select where the record is 26 days from expiring. Right now I'm using this which is not working. No records were selected.
SELECT * FROM `client` WHERE `expiry_date` = DATE_ADD(NOW(), INTERVAL 26 DAY)
I've searched this website and many of the answers are using <= operator. This solution partially work. It selects both of my record when I only need 2015-04-30 04:15:29 in expiry_date column.
How do I exactly select date that is going to expired and not all date?
The easy solution to this is to use the date function:
WHERE DATE(expiry_date) = DATE_ADD(CURRENT_DATE, INTERVAL 26 DAY)
However, this prevents the use of an index on expiry_date. An alternative that does work with indexes is:
WHERE expiry_date >= DATE_ADD(CURRENT_DATE, INTERVAL 26 DAY) AND
expiry_date < DATE_ADD(CURRENT_DATE, INTERVAL 26 + 1 DAY)
The reason you're having this issue is that expiry_date is a type of datetime so the time makes it not equal. Just change your code to be:
SELECT * FROM client WHERE DATE(expiry_date) = DATE(DATE_ADD(NOW(), INTERVAL 26 DAY))

SQL Date_Sub syntax error

I have been looking at this statement for ages and simply cannot find the error can you guys help?
SELECT XD.*, UhED.row_class,
(SELECT id
FROM Comment C
WHERE C.Excel_Data_Excel_Lists_id = XD.Excel_Lists_id
AND C.Excel_Data_row = XD.row
LIMIT 1
) AS has_activity
FROM User_has_Excel_Lists UhXL
JOIN Excel_Lists XL
ON XL.id = UhXL.Excel_Lists_id
JOIN Excel_Data XD
ON XD.Excel_Lists_id = XL.id
LEFT JOIN User_has_Excel_Data UhED
ON UhED.Excel_Data_Excel_Lists_id = XL.id
AND UhED.Excel_Data_row = XD.row
AND UhED.User_id = 1
WHERE UhXL.User_id = 1
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2)<-- it says that the error is here
GROUP BY XD.telephone
ORDER BY last_name ASC, first_name ASC
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY)
try this it may help you
You forgot do specify the interval unit maybe? Something like INTERVAL 2 DAY or INTERVAL 2 HOUR maybe?
You may need to specify the measure for the interval (e.g., days, months, etc.)
You can check the syntax for DATE_SUB here
if i understand your problem like follow this instructions as:
you should must mention the day, month, hour, or year in the following line:
your code:
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2)
change code: DAY
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY)
if you are using the DATE(tablename) in your table you getting error like:
WRONG CODE (sample)(http://sqlfiddle.com/#!2/5b7e2/7):check the code no:5
SELECT id,DATE_SUB(DAT(NOW()), INTERVAL 2 DAY)
FROM supportContacts;
RIGHT CODE:
SELECT id,DATE_SUB(DATE(NOW()), INTERVAL 2 DAY)
FROM supportContacts;
REFER THIS LINK: http://sqlfiddle.com/#!2/5b7e2/7

Cannot spot the difference between these two queries

Trying to clean up a long query I have come up with a modified version, but now the old query with certain parameters returns 6 rows, but my version returns none. I have been straining my eyes to spot the difference but cannot find any. Here are the WHERE clauses from the two versions with identical set of parameters:
The ugly (but working) version:
SELECT ...
FROM table as a
WHERE 1=1
and a.title LIKE '%Manager%'
and a.status='Approved'
and (a.effected_date<=now())
and (
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=0)
or
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=1)
)
My cleaned (but broken) version:
SELECT ...
FROM `table` AS `a`
WHERE CONCAT(`a`.`title`, ' | ', `a`.`job_detail_section`) LIKE '%Manager%' AND
`a`.`status` = 'Approved' AND
`a`.`effected_date` <= '2013-12-30' AND
(`a`.`effected_date` >= '2013-11-30' OR `a`.`is_hotjob` = '1')
You got the last AND/OR backwards:
`a`.`effected_date` <= '2013-12-30' OR
(`a`.`effected_date` >= '2013-11-30' AND `a`.`is_hotjob` = '1')
EDIT: after reformatting, the answer looks more like:
SELECT ...
FROM `table` AS `a`
WHERE CONCAT(`a`.`title`, ' | ', `a`.`job_detail_section`) LIKE '%Manager%' AND
`a`.`status` = 'Approved' AND
`a`.`effected_date` <= now() AND
`a`.`effected_date` >= DATE_ADD(a.effected_date, INTERVAL 30 DAY)
and POSSIBLY:
AND `a`.`is_hotjob` IN (0,1)
As a substitute of-
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=0)
or
(DATE_ADD(a.effected_date, INTERVAL 30 DAY) >= now() AND a.is_hotjob=1)
You could use the following-
(`a`.`effected_date` >= '2013-11-30' AND `a`.`is_hotjob` in (0, 1))
But according to the additional info i.e. a.is_hotjob has value either 0 or 1 then there is no need to mention it in the condition at all. So basically the following format will be sufficient-
`a`.`effected_date` >= '2013-11-30'

How Do I Add 2 Hours from Formatted Time

I need to know how to add 2 hours to the below 'Completed' timestamp.
Here is the Select statement
Select Tsk.task_id,Tsk.org_id,Tsk.completed,Tsk.assgn_acct_id,name
FROM tdstelecom.tasks As Tsk
WHERE Tsk.task_id = '11094836'
AND DATE(Tsk.completed) < CURDATE() AND DATE(Tsk.completed) >= DATE_SUB(CURDATE
(),INTERVAL 180 DAY)
Here are the results: 2012-08-22 14:18:14
Desired results: 2012-08-22 16:18:14
Your tag says mySQL, use the subtime(exp1,exp2) function SUBTIME(Tsk.completed, '02:00:00.000000') in your select should do the trick.
select Tsk.completed+interval 2 hour,Tsk.assgn_acct_id,name
FROM tdstelecom.tasks As Tsk
WHERE Tsk.task_id = '11094836'
AND DATE(Tsk.completed) < CURDATE() AND DATE(Tsk.completed) >= DATE_SUB(CURDATE
(),INTERVAL 180 DAY)