SQL Server - exclude data where there's no connection to second table - sql-server-2008

The example below shows the result for every Name that has a connection to Table2 (Table1 TId is PK, and TId in Table2 is the FK).
SELECT T1.Name, T1.Address
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.TId = T2.TId;
I want a list of all Names from Table1 that have NO corresponding row in Table2. The other way around so to speak. How could this be done?

You need to use an Outer Join as shown below:
SELECT T1.Name, T1.Address
FROM Table1 AS T1
LEFT OUTER JOIN Table2 AS T2 ON T1.TId = T2.TId
WHERE T2.TId IS NULL

Related

mySQL group two INNER JOINs

I basically want to join the result of two INNER JOINs.
On this scheme I want to get the three arrows results combined.
I've tried INNER / LEFT combinations but it doesn't do the trick.
I think a nested request could be the solution but how ?
Thanks
The answer was actually simple : UNION
SELECT t1.*
FROM
(SELECT t1.*
FROM table1 t1 JOIN table2 t2 ON t2.id = i.client_id
UNION
SELECT t1.*
FROM t1 t1 JOIN table3 t3 ON t1.id = t3.client_id) as q1
;
I'd use logic to express the condition T1.id exists in T2 or T3 more directly, and certainly avoid use of DISTINCT or UNION.
Options could be to use EXISTS directly (As this is immure to the possibility of duplication cause by 1:many joins)...
SELECT
t1.*
FROM
table1 t1
WHERE
EXISTS (SELECT * FROM table2 t2 WHERE t2.t1_id = t1.id)
OR
EXISTS (SELECT * FROM table3 t3 WHERE t3.t1_id = t1.id)
Or to LEFT JOIN twice and then exclude unwanted rows. (This assumes that the joins are never 1:many, which would introduce duplication, and the unwanted need for a DISTINCT.)
SELECT
t1.*
FROM
table1 t1
LEFT JOIN
table2 t2
ON t1.id = t2.t1_id
LEFT JOIN
table3 t3
ON t1.id = t3.t1_id
WHERE
t2.t1_id IS NOT NULL
OR
t3.t1_id IS NOT NULL

mysql foreign key foriegn key relationship

Say, myself having three MySQL tables as follows.
Table 1 contains table1id, value columns.
Table 2 contains table2id, value, table1id (as FK) columns.
Table 3 contains table3id, value, table1id (as FK) columns.
Then is the following relationship valid?
select * from table1 t1 inner join table2 t2 on t1.table1id = t2.table1id
Yes, It is possible.
This one is Joining for teable1 and table2
select * from test1 t1 inner join test2 t2 on t1.id = t2.id;
This one is joining all three tables,
SELECT * FROM test1 t1
INNER JOIN test2 t2 ON t1.id = t2.id
INNER JOIN test3 t3 ON t1.id = t3.id;
Output: ONLINE DEMO HERE
Try this
SELECT * FROM
table1 t1 INNER JOIN table2 t2
ON t1.table1id = t2.table2id
INNER JOIN table3 t3
ON t1.table1id = t3.table3id
You can also write it this way:
SELECT * FROM table1 t1, table2 t2, table3 t3
WHERE (t1.table1id=t2.table2id) AND (t1.table1id=t3.table3id);
** If you want to join only the first 2 tables--Use the code until the AND
*** If you want to join all the tables-- Use the entire code.

Retrieve data from two tables sql

I am having 2 tables named table1, table2.
table1 columns are t1_id, t1_name, t1_status.
table2 columns are t1_id, t2_id, t2_name, t2_status.
When I do Some operation in frontend t1_id will be inserted into table2.
What I need is I want t1_id(s) from table1 which are not alloted or inserted in table2.
I've tried this query:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id != t2.t1_id
The problem with query is when all t1_id(s) are inserted into table2, then all t1_id(s) are showing again.
How to resolve this issue? (I'am new to sql so please don't consider my mistakes.)
If i understand your question right, this must b the query you need:
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id = t2.t1_id
where t2.t1_id is null
Use is null to get rows from table1 which don't have any associations in table2
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.t1_id = t2.t1_id
WHERE t2.t1_id IS NULL
I think this answers your questions: Select rows which are not present in other table
SELECT t1.t1_id, t1.t1_name
FROM table1 t1
WHERE NOT EXISTS (
SELECT 1 --it's not so relevant what you put here
FROM table2 t2
WHERE t1.t1_id = t2.t1_id
)
I hope this helps. ;-)

SQL query to find unreferenced records

I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )
I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;
You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'

One SQL statement for two Foreign Keys

I have two tables. The first table contains ID, First_Name and Last_Name.
The 2nd table contains two foreign key fields containing different ID's of the first table.
I want to be able to run a SQL query that gets reults of the 2nd table which then grabs the First_Name of each member based on the two different foreign keys.
How would I go about doing this?
select t2.*, t1a.firstname, t1b.firstname
from table2 t2
left join table1 t1a on t2.fk1 = t1a.id
left join table1 t1b on t2.fk2 = t1b.id
Suppose the second table has fields as such
userid, supervisorid ( both referring to the Id column of the first table )
you may write join to get the value like this
SELECT t2.*, ID, firstname, lastname FROM table 2 t2
LEFT OUTER JOIN table 1 t1 ON
t2.userid = t1.id
OR t2.supervisorid = t1.id
I think correct sql would be below one using OR condition in outer join or using union
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id1
UNION
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id0
SELECT t1.id, t1.name from table2 t2 LEFT OUTER JOIN table1 t2 ON t1.id = t2.id or t1.id1 = t2.id0