Conditional SELECT SQL with relational tables - mysql

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);

Related

Join two tables based on a view, sql

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.

Select distinct rows based on column and two conditions in SQL

I would like to retrieve rows from this data set where a T1/T3 value exists, but no T2/T3 value exists for a corresponding ID.
ID sample1 sample2 value
A_000002 T2 T3 -0.934119
A_000002 T1 T3 -0.866637
A_000029 T2 T3 -1.07677
A_000037 T2 T3 -0.76506
A_000057 T1 T3 -5.34988
I'd like to say something like:
SELECT * FROM table
WHERE DISTINCT ID
AND sample_1 == "T1"
AND sample_2 == "T3"
...and return only the following because it has no corresponding T2/T3 row for that ID:
A_000057 T1 T3 -5.34988
If I use sample_1 and sample_2 conditions, I get distinct values anyway because it filters out the the "T2" values before checking if the ID is distinct.
The closest I've come is to make 3 tables with the possible T1/T2/T3 combinations and screen for NOT EXISTS T1T2.ID = T2T3.ID
select * from T1T2
where not exists (select * from T2T3 where T2T3.id = T1T2.id)
and not exists (select * from T1T3 where T1T3._id = T1T2.id)
order by id
I'm not sure I trust the code yet though.
You can use not exists :
select t.*
from table t
where (sample1 = 'T1' and sample2 = 'T3') and
not exists (select 1
from table t1
where t1.id = t.id and
t1.sample1 = 'T2' and t1.sample2 = 'T3'
);
You can use this technique based on an outer join:
select t1.*
from table t1
left join table t2
on t2.id = t1.id
and t2.sample1 = 'T2' and t2.sample2 = 'T3'
where t1.sample1 = 'T1' and t1.sample2 = 'T3'
and t2.id is null
It works because outer joins return nulls if there's no join, and you return only those via the t2.id is null condition in the where clause.
The big advantage of this technique is the efficient use of the index on the id column; this technique will generally out-perform other apporaches. Plus IMHO it's a neater looking query.
Note that the condition for the sample columns must be in the join condition for t2, otherwise you'd effectively get an inner join, defeating the required outer join.
I would use not exists:
SELECT t.*
FROM table t
WHERE t.sample_1 = 'T1' AND t.sample_2 = 'T3' AND
NOT EXISTS (SELECT 1 FROM table t2 WHERE t2.id = t.id);
Here will surly work:
Select distinct ID, * from table
Where sample1 = 't1' and sample2 = 't3'

How to query a date between two other dates in another table

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);

joining tables with where clause

I am having two tables, the structure is given below
Table 1
schid
name
cost
type
Table 2
schid
details
oldcost
I am unable to write a query to display records from table 2 of let suppose type A OR B (Here as you can see type field is in table 1), Here one more thing to add is that schid is not a primary key, The query which i am executing is retrieving more records than expected, I think due to join, Can i execute it without using join
SELECT *
FROM Table1
JOIN Table2 ON Table1.schid=Table2.schid
WHERE Table1.type='A'
OR Table1.type='B'
This would help:
SELECT t2.schid, t2.details, t2.oldcost
FROM Table2 t2
JOIN Table1 t1
ON t1.schid = t2.schid
WHERE t1.type IN ('A', 'B');
This should retrieve only the table 2 records which match the criteria.
SELECT t2.*
FROM Table2 t2
JOIN Table1 t1 ON t1.schid = t2.schid
WHERE t1.type = 'A'
OR t1.type = 'B';
SELECT t2.*
FROM `Table2` t2
JOIN `Table1` t1 ON t2.`schid`=t1.`schid`
WHERE t1.`type` IN ('A','B');

query data from one table that matches with value from another table

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)