How to select all records in the table t2 which t2.t1_id has no coincidence with t1.id.
SELECT * FROM t2 LEFT JOIN t1 ON t1.id <> t2.t1_id
Any tips, link or code example would be useful.
If what you want is all t2 records without a matching id in t1, but no columns from t1, you could do:
Select * from t2
WHERE t2.t1_id NOT IN(Select id from T1)
This selects all records in t2, but then filters out those that exist in t1 based on t1_id
You can use a not in:
SELECT *
FROM t2
WHERE t2.t1_id not in (select id from t1)
SELECT t2.*
FROM t2
LEFT JOIN t1
ON t1.id = t2.t1_id
where t1.id is null
Just want to add, NOT EXIST is better in most cases:
SELECT *
FROM t2
WHERE NOT EXIST (SELECT 1 FROM t1
WHERE t2.t1_id = t1.id)
Otherwise, you can use NOT IN or LEFT JOIN with NULL
Related
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
I had a problem creating a MySQL query with a subquery.
I wanted to use some data from the main query on the subquery, as many times did.
But this time I wanted to use it in a JOIN and didn't worked. I really want to understand why this happens.
I will show you some examples that works and the one that didn't.
I made this simple structure to reproduce the example:
# table1
id field1
1 *first_value*
2 *another_value*
#table2
id field2
1 *second_value*
Using table1.id on the WHERE of the subquery to get a value, the most typical use for me (I know this can be a join, but i try to show the difference):
SELECT
t1.field1,
(
select t2.field2
FROM table2 as t2
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';
You can use table1.id on the SELECT part too (not much sense in the example, but works):
SELECT
t1.field1,
(
select t1.id as field2
FROM table2 as t2
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';
Now, if you try to use it on a JOIN inside the subquery, then, crashes:
SELECT
t1.field1,
(
select t1.id
FROM table2 as t2
LEFT JOIN table1 as t3 ON t3.id = t1.id
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';
Kernel error: Error( 1054 ) 42S22: "Unknown column 't1.id' in 'on clause'"
Buuut, u can do the JOIN using the field in another subquery changing ON t3.id = t1.id to ON t3.id = (SELECT t1.id) ???
SELECT
t1.field1,
(
select t1.id
FROM table2 as t2
LEFT JOIN table1 as t3 ON t3.id = (SELECT t1.id)
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1'
I wonder to know why the third example query doesn't work while all others does.
Can someone explain this, please?
Thank you :)
That's because all elements in the ON clause of a JOIN, must belong the one of the joined tables, so as your t2.id must be equal to t1.id, you can do
SELECT
t1.field1,
(
select t1.id
FROM table2 as t2
LEFT JOIN table1 as t3 ON t3.id = t2.id
WHERE t2.id = t1.id
) as field2
FROM table1 as t1
WHERE t1.id = '1';
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. ;-)
I have a query which contains a select statement in it's where clause. My question is now, how can I access the parent's select's data.
Example:
select * from TABLE_1 as t1 INNER JOIN TABLE_2 as t2
where (... and ...) OR
(not exists(select * from TABLE_3 as t3
inner join TABLE_1 ON t3.t1_id = t1.id
The last line is where the error occurs: t1.id is not a column.
How can I access the current value from the table t1?
I'm using MySql 5.1
SELECT
*
FROM
TABLE_1 as t1
INNER JOIN TABLE_2 as t2 ON
t2.PK = t1.FK --Whatever your keys are
WHERE
(... and ...)
OR
(
NOT EXISTS (select * from TABLE_3 as t3 WHERE t3.t1_id = t1.id)
)
First of all, you need to declare what you will JOIN TABLE_2 on TABLE_1.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
t1.id = t2.t1_id is just an example, you will need to decide which columns you wish you join on. Then in your WHERE clause, you do not need to INNER JOIN on TABLE_1 again as you are already selecting from it.
SELECT *
FROM TABLE_1 AS t1
INNER JOIN TABLE_2 AS t2 ON t2.t1_id = t1.id
WHERE (... AND ...) OR
(
NOT EXISTS
(
SELECT *
FROM TABLE_3 AS t3
WHERE t3.t1_id = t1.id
)
)
I have the 2 following tables t1, t2 with values,
t1 t2
1 4
2 2
3 3
Now I want to output
1
4
How can I get this output in select query ?
This will get you each item from t1 that is not present in t2, and each item in t2 that is not present in t1:
select t1.id from t1
left join t2 on t2.id = t1.id
where t2.id is null
union all
select t2.id from t2
left join t1 on t1.id = t2.id
where t1.id is null
(I have assumed that the field name in each table is named id just for the sake of being able to write a query against the tables.)
Another way would be:
select coalesce(t1.id, t2.id)
from t1
full outer join t2 on t2.id = t1.id
where t1.id is null or t2.id is null
Another way. Just COUNT them.
This works if the values are unique per table
SELECT
CombinedValue
FROM
(
SELECT t1 AS CombinedValue FROM t1
UNION ALL
SELECT t2 FROM t2
) foo
GROUP BY
CombinedValue
HAVING
COUNT(*) = 1
If not unique per table
SELECT
CombinedValue
FROM
(
SELECT DISTINCT t1 AS CombinedValue FROM t1
UNION ALL
SELECT DISTINCT t2 FROM t2
) foo
GROUP BY
CombinedValue
HAVING
COUNT(*) = 1
you can use Joins in MySql to proceed and to obtain result.
this will help you
http://www.techrepublic.com/article/sql-basics-query-multiple-tables/1050307