I have TWO tables:
t1 contain ID_car (unique), name
t2 contain ID_car(from t1), status (many status records on same ID_car)
And i need the following result:
All ID_car FROM t1 WITHOUT status = something
I already try it INNER, LEFT, RIGHT JOIN and didn't work.
How can i do that?
Many thanks for help!
More details:
t1
------------
ID_car name
------------------
1 Toyota
2 Honda
3 Mazda
4 Ford
t2
-----------------
ID_car status
1 ok
1 not_ok
2 ok
4 not_ok
ID_car 3 din not have any records in t2 but i want to display result
And i need the following result (all car from t1 without car status not_ok):
the expected result
-----------------
ID_car status
2 ok
3
Update 2
Finally solved! Thanks for help!
That's works for me:
SELECT * FROM t1
WHERE t1.ID_auto NOT IN
(SELECT DISTINCT t1.ID_auto FROM t1, t2 WHERE t1.ID_auto = t2.ID_auto AND t2.category='not_ok')
-- updated per comment below. This update will return all records from T2 (even if not existing in T1) but only if T2.Status != something
-- this should do what you want. It will give you all the records in T1, and any data in T2 (but not required to be in that table) where your T1.status is not something
SELECT *
FROM t1
LEFT JOIN t2 ON T1.ID = T2.ID
-- did the logic on the JOIN here instead of in where clause, because doing in where clause would force records to appear in t2 table (basically converting it to an inner join) doing it in the join itself does not cause this to happen
AND T2.Status != 'something'
SELECT ID_car
FROM t1
LEFT JOIN t2
ON t1.ID_car=t2.ID_car
WHERE t2.status=something
SELECT ID_car
FROM t1
LEFT JOIN
t2 on t1.ID_car=t2.ID_car
WHERE NOT t2.status='something'
Created from top of my head, hopefully it works!
Maybe if you could post the queries you wrote, maybe they could show us why the joins don't work because it should.
I do not know if I understand correctly but try and let me know:
SELECT DISTINCT t1.ID_car FROM t2 INNER JOIN t1 ON t2.ID_car = t1.ID_car WHERE t2.status != 'something'
Related
I am a bit stunned that I am no able to produce my results I want to get so I ask you experts for help!
I have three Tables showing only the important parts:
T1: (List of all names and schedules assigned)
Name | ScheduleName
T2: (all possible schedule names)
ScheduleName
T3: (List of all names)
Name
I try to find the ScheduleName per Person that was not assigned. In the end I would like to see:
Name1 | ScheduleName1
Name1 | ScheduleName2
Name1 | ScheduleName3
Name2 | ScheduleName2
What I tried:
SELECT
T2.ScheduleName
FROM
T2
WHERE
T2.ScheduleName NOT IN
(SELECT T1.ScheduleName FROM T1 WHERE T1.Name = "Name1")
This gives me the not assigned schedules for Person Name1. Works.
If I remove the WHERE statement within the parenthesis it returns no rows. (Because it matches it against the whole dataset)
Help very much appreciated
Thanks in advance
EDIT: Tried to work with a LEFT JOIN but it is not returning rows with "NULL" although it should
Here is one option, using a cross join:
SELECT
T3.Name,
T2.ScheduleName
FROM T2
CROSS JOIN T3
LEFT JOIN T1
ON T2.ScheduleName = T1.ScheduleName AND T3.Name = T1.Name
WHERE
T1.Name IS NULL;
The idea behind the cross join is that it generates all possible pairings of names and schedules. That is, it represents the entire set of all pairings. Then, we left join this to the T1 assignment table to find pairings which were never made.
We could also have achieved the same thing using EXISTS logic:
SELECT
T3.Name,
T2.ScheduleName
FROM T2
CROSS JOIN T3
WHERE NOT EXISTS (SELECT 1 FROM T1
WHERE T2.ScheduleName = T1.ScheduleName AND T3.Name = T1.Name);
umm I'm not sure I've made the title right but its kind of hard to express it in short words.
I have to tables
table1:
id | name
1 | alice
2 | bob
table 2:
user_id | date
2 | 2014-11-1
2 | 2014-11-2
1 | 2014-11-3
as a query, if I want to show table 2 but instead of the integer numbers of user_id, I want it to show the corresponding names of the users where this info is stored in table 1.
I think this is supposed to be easy but I don't know how to get this done.
A query along the lines of -
select t1.name, t2.date
from table_1 t1 inner join table_2 t2 on t1.id = t2.user_id
Try:
SELECT t2.user_id, t1.name
FROM table1 t1 INNER JOIN table2 t2
ON t1.id = t2.user_id
This will do it.
SELECT
`b`.`name`,
`a`.`date`
FROM
table2 a
INNER JOIN table1 b ON (a.user_id = b.id)
SELECT
B.[Name]
,A.[date]
FROM [table 2] A
LEFT OUTER JOIN [table1] B
ON A.[user_id] = B.[id]
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.
Below is my Table Structure
Table 1
id
name
Table 2
id
table1_id
I want the rows from table 1 which have no reference value in table 2.
Example data:
Table 1
id name
1 demo
2 demo2
3 demo3
Table 2
id table1_id
1 1
2 1
3 1
4 3
5 3
So there is no value in table 2 with table1_id 2. I want id 2 from Table 1.
Below id the query i have tried:
SELECT l.id FROM Table1 l WHERE l.id NOT IN (SELECT DISTINCT(r.id) FROM table2 r);
This is returning a proper result but its taking more than 2 minutes to process.
In table 1 i have 4000 rows and in table 2 I have 40000 rows.
Any optimisation to above query or any alternative solution?
SELECT * FROM table1 LEFT JOIN table2
ON table1.id=table2.table1_id
WHERE table2.table1_id IS NULL
Have an index for Table1.id and Table2.table1_id, then try the following query:
SELECT Table1.id FROM Table1
WHERE Table1.id NOT IN (SELECT Table2.id FROM Table2 group by Table2.table1_id);
What you are trying to acheive is to find orphan records right?
A join that shows all the records from the first(the left) table and the matching values form the other or nulls for no matches is called a left join. I think a left join will do the same job but it is not going to be any faster. Joins are in general slower.
I found a place where it is all well explained - http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
It does not hurt to try with a join though, and tell us were your results the same as expected.
select t1.id from table1 as t1
left outer join table2 as t2
on t2.table1_id = t1.id
where t2.id is null;
or
select t1.id from table1 as t1
where not exists (select 1
from table2 as t2 where t2.table1_id = t1.id);
i am having table structure like this
table1 table2
pid pname pid uid cat
1 a 1 1 1
2 b 1 2 1
3 c 1 3 1
select * from table1 as t1 LEFT JOIN table2 as t2 on t1.pid=t2.pid where t2.uid=1 AND t2.cat=1
it's select the two rows
i don't want to group pid because i may have same pid in table one so i need to get only number of rows matched with pid
This may be silly question but i tried hard i couldn't get anything .
I hope you people can help me!
Thanks in advance
If you want to get the count of rows that match:
SELECT COUNT(*)
FROM table1 t1 INNER JOIN table2 t2 on t1.pid = t2.pid
WHERE t2.uid=1
Your LEFT join is unnecessary, since you are filtering by table2.
http://en.wikipedia.org/wiki/Join_%28SQL%29