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);
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
I have the following sql situation
Table1
id name
Table2
id name
_Table1ToTable2
id, id_table1, id_table2
Result:
id, name, table2_name, table2_id
I would like to join Table1 and Table2 into a single query, but I cannot find a way to make a join between them, any ideas?
It seems like you want to use bridge table Table1ToTable2 to join Table1 and Table2. This can be done simply with 2 INNER JOINs :
SELECT
tt.id AS tt_id,
t1.id AS t1_id,
t1.name AS t1_name,
t2.id AS t2_id,
t2.name AS t2_name
FROM
Table1ToTable2 AS tt
INNER JOIN Table1 AS t1
ON t1.id = tt.id_table1
INNER JOIN Table2 AS t2
ON t2.id = tt.id_table2
if there is no relationship between table1 and table 2 then,
select table1.id, table1.name, table2.id, table2.name from
table1, table2
if table1 and table2 are related with ID then,
select table1.id, table1.name, table2.id, table2.name from
table1
inner join table2
on table1.id = table2.id
If your intention is to retrieve a result set with every possible combination of the id columns from both tables then you can do as follows:
select id = identity (int, 1, 1), * into #a from (select distinct table1. id as id1, table2. id as id2 from table1 cross apply table2) d; select * from #a; drop table #a
If you do not wish to use a temp table an alternative would be to use some variation of the row_number() function.
The above solves for an assumed requirement as your question lacks context. Elaborate on your requirements and we will be able to provide you with a more relevant and detailed answer.
Say I have an id in table1 which is a foreign key in table2 and there is a column in table2 called condition.
I need to select all ids from table 1 that aren't in table2 where condition = 1.
So for id in table 1 "select it" if it is not in table2 where condition = 1.
Edit: I used Ahsan Habib's answer and it worked great!
if you just want to select ID column from table1 this will work fine....Its just a simple set operation
select id from t1
minus
select id from t2 where condition = 1;
for all column you may try
select * from t1 whare id not in (select id from t2 where condition = 1);
This is almost a direct translation of what you are asking for:
select t1.*
from table1 t1
where t1.id not in (select t2.id from table2 t2 where t2.condition = 1);
Another way using NOT EXISTS
select t1.*
from table1 t1
where NOT EXISTS(select 1 from table2 t2 where t1.id = t2.id and t2.condition = 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.
I have two table created as
create table table1(id int,);
create table table2(id int, tb2_id int,...)
But when i try out
Select * from table2 where tb2_id=table1.id;
I have got an error that table1.id is an unknown column.
Could someone point out where the mistake I made is ?
You probably want to JOIN tables:
SELECT table2.* FROM table2 JOIN table1 ON (table2.tb2_id=table1.id)
Select * from table2, table1 where tb2_id=table1.id;
You need either a join or a subquery.
Select t2.*
from table2 t2
Inner join table1 t1
On t2.tbl2_id = t1.id
Or
Select t2.*
from table2 t2
where tbl2_id in ( select id from table1 )
try this:
SELECT *
FROM Table2
WHERE ID IN (SELECT ID FROM Table1)