Been trying to figure this out for a while now.
I am looking to select rows from a database table in MySQL where the two dates are next to each other.
e.g. 2011-07-20 is next to 2011-07-21.
Many Thanks
Neil
I guess you can JOIN using ADDDATE():
SELECT T1.id, T2.id
FROM myTable AS T1
INNER JOIN myTable AS T2
ON T1.DATE = ADDDATE(T2.DATE, -1)
WHERE T1.id < T2.id;
The WHERE is to verify that T1 and T2 don't contain duplicates.
SELECT * FROM table WHERE date_column BETWEEN '2011-07-20' AND DATE_ADD('2011-07-20', INTERVAL 1 day)
Manual:
between operator
date_add function
Related
I have two table:
Can't run query, to display "Result" table, pls help.
Consider:
SELECT
t1.id,
t2.id1,
t2.col
FROM table1 t1
INNER JOIN table2 t2
ON t1.nr1 BETWEEN t2.MinNr1 AND t2.MaxNr1
AND t1.nr2 BETWEEN t2.MinNr2 AND t2.MaxNr2
Work only with:
INNER JOIN table2 t2
On t1.nr1 >= t2.MinNr1 And t1.<= t2.MaxNr1
And t1.nr2 >= t2.MinNr2 And t2.MinNr2 <= t2.MaxNr2
hello i am trying to search for duplicates in a table within a period using this snippet.
FROM
table WHERE Start_Date BETWEEN '2018-07-01' AND '2018-07-31'
GROUP BY Policy_Number
HAVING COUNT(Policy_Number) > 1;
this produces all the duplicates records in the table within the required dates with their counts.
Now i am trying to delete those duplicate records using this snippets using this snippet i have also found online
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND Start_Date BETWEEN '2018-07-01' AND '2018-07-31';
but i keep getting this error
Column 'Start_Date' in where clause is ambiguous
Please how can i correct this to delete the duplicates i want removed thanks!!
write like this way t1.Start_Date its work
Try running as a query previous executing your select:
SET SQL_BIG_SELECTS=1;
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND t1.Start_Date BETWEEN '2018-07-01' AND '2018-07-31';
Because you create t1 and t2 using same table table so both have start_date and thats why its give Column 'Start_Date' in where clause is ambiguous error
Define the alias before start_Date
SET OPTION SQL_BIG_SELECTS = 1
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND t1.Start_Date BETWEEN '2018-07-01' AND '2018-07-31'
an example works
SELECT
table.Number
FROM table
INNER JOIN table2
ON table.ID = table2.ID
WHERE checkInDate BETWEEN '2015-09-12' AND '2015-09-13';
I need to query a date with a value between two other dates that come from another table. Something like this:
select * from table1 where date_table1 BETWEEN (select date1,date2 from table2 where id=1)
How can I do that in SQl?
Just use join or exists. As you have written it:
select t1.*
from table1 t1
where exists (select 1
from table2 t2
where t1.date between t2.date1 and t2.date2
);
I am not sure what you are trying to do but the way you phrased it the question seems incomplete. Based on what you have provided, I think something like this will work:
select t1.*
from table1 t1
inner join table2 t2 on t2.date1 <= t1.date_table1 and t1.date_table1 < t2.date2
where t2.somefield = [somelimitingcondition]
This should work provided each sub-query returns exactly one value
SELECT * from table1 t1
WHERE t1.date_table1
BETWEEN
(SELECT t2.date1 from table2 t2 WHERE t1.id=1)
AND (SELECT tt2.date2 from table2 tt2 WHERE t1.id=1);
I have two tables from which I need to get data in the same SELECT output. The thing is that I need to limit the amount of results.
Say I have an ID column that is unique in table1, but in table2 it has many rows with that ID.
Now I just want to list how many different IDs I have in table1 and some other information stored in table2.
How can I get the desired output I show in the end?
To make my idea clear I used a "messenger" database for an example.
Tables
T1
Id_thread Date
1 13Dic
2 12Dic
T2
Id_thread Message Name
1 Hi Someone
1 Hi to you Someone
2 Help me? Someother
2 Yes! Someother
Desired output
T1.Id_thread T2.Name T1.Date
1 Someone 13Dic
2 Someother 12Dic
I'd join and use distinct:
SELECT DISTINCT t1.id_thread, t2.name, t1.date
FROM t1
JOIN t2 ON t1.id_thred = t2.id_thread
Use a JOIN and GROUP BY:
SELECT t1.Id_thread, t2.Name, t1.Date
FROM t1
JOIN t2 ON t1.Id_thread = t2.Id_thread
GROUP BY t1.Id_thread
Note that if Name is the same for all rows in t2 that have the same Id_thread, that column probably should be in t1. If you fix that, you don't need the JOIN.
Try this:
SELECT DISTINCT T1.Id_thread, T2.Name, T1.Date
FROM T1
LEFT OUTER JOIN T2 ON T1.Id_thread = T2.Id_thread
select T1.Id_thread,T2.Name,T1.Date from T1
inner join T2 on T1.Id_thread = T2.Id_thread
group by T1.Id_thread
order by T1.Id_thread
You haven't specified how you want the limit the results from Table 2. Considering you just want one row, you can use a CROSS APPLY:
Select T1.Id_thread,T2Table.Name,T1.Date From T1
Cross Apply (Select Top 1 T2.Name From T2 Where T2.Id_thread=T1.Id_thread) T2Table
You can specify other conditions in the inner Select statement if you wish.
Is is possible to construct a SELECT statement that contains a WHERE
clause that uses the value from a column in the "next" row?
I have a date and time field date_entered.
ie. given a table with a field named "date_entered" with the
following values I want a SELECT statement that selects the rows
"WHERE date_entered>='current_date' and date_entered<=[NEXT ROW]date_entered "
I tried Post but not found what I am looking at.
If your records are ordered chronologically, then you may get through with a query like this:
SELECT
t1.*,
min(t2.date_entered) as next_row_date
from mytable t1
join mytable t2
/* ON nothing */
where t1.id < t2.id
and t1.date_entered > NOW()
group by t1.id
having t1.date_entered <= next_row_date
If you really need next row date... You will likely get a better result with a subquery. Something like
SELECT
t1.*,
from mytable t1
where t1.date_entered > NOW()
and t1.date_entered < ( SELECT date_entered from mytable t2 where t2.id > t1.id limit 1)