Multiple search with join - mysql

I'm trying to search a rows of table3 that have id match in a table1 OR table2 with this query
SELECT T1.*, T3.*,T2.*
FROM (
select id
from table1
where condition like '%field%') T1
inner join table3 T3
ON T3.id=T1.id
left join (
select id
from table2
where condition like '%field%') T2
ON T3.id=T2.id
If in table T1 have matches but in table2 not, the query works fine but if in table1 not have matches but have in table2 the query not show any results.
Someone can help me?
Thanks

do left join to avoid excluding rows, and add a where with the condition you need FOr readability sake,avoid right joins and jsut use proper table order
SELECT T1.*, T3.*,T2.*
FROM Table3 T3
left join (
select id
from table1
where condition like '%field%') T1
ON T3.id=T1.id
left join (
select id
from table2
where condition like '%field%') T2
ON T3.id=T2.id
where t3.id is not null or t2.id is not null

Related

Finding Entries Without Match [duplicate]

I have these MySQL tables:
table1:
id | writer
1 | Bob
2 | Marley
3 | Michael
table2:
user_one | user_two
1 | 2
And this query:
SELECT table1.id FROM table1 LEFT JOIN table2 ON table1.id = table2.user_one
This query will return all rows of table1 which are 1,2,3
I want to select only rows which are not found in the left joint. So it should return only row with id 3
I want sort of the opposite of INNER JOIN which will select only the rows which are found in the join. How to get the opposite like if left join exists, ignore it and move to the next row. Hope i'm clear
You could use the following query:
SELECT table1.id
FROM table1
LEFT JOIN table2
ON table1.id IN (table2.user_one, table2.user_two)
WHERE table2.user_one IS NULL;
Although, depending on your indexes on table2 you may find that two joins performs better:
SELECT table1.id
FROM table1
LEFT JOIN table2 AS t1
ON table1.id = t1.user_one
LEFT JOIN table2 AS t2
ON table1.id = t2.user_two
WHERE t1.user_one IS NULL
AND t2.user_two IS NULL;
One of the best approach if you do not want to return any columns from table2 is to use the NOT EXISTS
SELECT table1.id
FROM table1 T1
WHERE
NOT EXISTS (SELECT *
FROM table2 T2
WHERE T1.id = T2.user_one
OR T1.id = T2.user_two)
Semantically this says what you want to query: Select every row where there is no matching record in the second table.
MySQL is optimized for EXISTS: It returns as soon as it finds the first matching record.
One more note to this:
When you check against a nullable column in the joined table, depending on your needs, you may have to use NOT EXISTS (or check against other columns when using LEFT JOIN), because MySQL won't be able to distinguish between a column which is NULL, but there is an existing record in the joined table and a column which is generated as NULL because the joined table have no matching record.
Here is a query that returns only the rows where no correspondance has been found in both columns user_one and user_two of table2:
SELECT T1.*
FROM table1 T1
LEFT OUTER JOIN table2 T2A ON T2A.user_one = T1.id
LEFT OUTER JOIN table2 T2B ON T2B.user_two = T1.id
WHERE T2A.user_one IS NULL
AND T2B.user_two IS NULL
There is one jointure for each column (user_one and user_two) and the query only returns rows that have no matching jointure.
Hope this will help you.
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table1.id = table2.user_one
WHERE table2.user_one is NULL
Try following query:-
SELECT table1.id
FROM table1
where table1.id
NOT IN (SELECT user_one
FROM Table2
UNION
SELECT user_two
FROM Table2)
Hope this helps you.
Try:
SELECT A.id FROM
(
SELECT table1.id FROM table1
LEFT JOIN table2 ON table1.id = table2.user_one
WHERE table2.user_one IS NULL
) A
JOIN (
SELECT table1.id FROM table1
LEFT JOIN table2 ON table1.id = table2.user_two
WHERE table2.user_two IS NULL
) B
ON A.id = B.id
See Demo
Or you could use two LEFT JOINS with aliases like:
SELECT table1.id FROM table1
LEFT JOIN table2 A ON table1.id = A.user_one
LEFT JOIN table2 B ON table1.id = B.user_two
WHERE A.user_one IS NULL
AND B.user_two IS NULL
See 2nd Demo

Inner join one to many unique_id between two table

one table t1 has one unique id u1
another table t2 has three unique_id u2a, u2b, u2c
How to make an inner join between two tables so that
u1 of table t1 join either u2a, u2b or u2c of table t2.
I don't think this is a best practice but try:
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c;
Demo: https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/116
If you want the name in order you can add order by name desc; in the end of the query.
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2a
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2b
union
select t1.name,t2.country
from table1 t1
inner join table2 t2
on t1.u1=t2.u2c
order by name desc;

how to return all the fields of table2 based upon the occurrence of the id in the table1

I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;

MySQL select rows where left join is null

I have these MySQL tables:
table1:
id | writer
1 | Bob
2 | Marley
3 | Michael
table2:
user_one | user_two
1 | 2
And this query:
SELECT table1.id FROM table1 LEFT JOIN table2 ON table1.id = table2.user_one
This query will return all rows of table1 which are 1,2,3
I want to select only rows which are not found in the left joint. So it should return only row with id 3
I want sort of the opposite of INNER JOIN which will select only the rows which are found in the join. How to get the opposite like if left join exists, ignore it and move to the next row. Hope i'm clear
You could use the following query:
SELECT table1.id
FROM table1
LEFT JOIN table2
ON table1.id IN (table2.user_one, table2.user_two)
WHERE table2.user_one IS NULL;
Although, depending on your indexes on table2 you may find that two joins performs better:
SELECT table1.id
FROM table1
LEFT JOIN table2 AS t1
ON table1.id = t1.user_one
LEFT JOIN table2 AS t2
ON table1.id = t2.user_two
WHERE t1.user_one IS NULL
AND t2.user_two IS NULL;
One of the best approach if you do not want to return any columns from table2 is to use the NOT EXISTS
SELECT table1.id
FROM table1 T1
WHERE
NOT EXISTS (SELECT *
FROM table2 T2
WHERE T1.id = T2.user_one
OR T1.id = T2.user_two)
Semantically this says what you want to query: Select every row where there is no matching record in the second table.
MySQL is optimized for EXISTS: It returns as soon as it finds the first matching record.
One more note to this:
When you check against a nullable column in the joined table, depending on your needs, you may have to use NOT EXISTS (or check against other columns when using LEFT JOIN), because MySQL won't be able to distinguish between a column which is NULL, but there is an existing record in the joined table and a column which is generated as NULL because the joined table have no matching record.
Here is a query that returns only the rows where no correspondance has been found in both columns user_one and user_two of table2:
SELECT T1.*
FROM table1 T1
LEFT OUTER JOIN table2 T2A ON T2A.user_one = T1.id
LEFT OUTER JOIN table2 T2B ON T2B.user_two = T1.id
WHERE T2A.user_one IS NULL
AND T2B.user_two IS NULL
There is one jointure for each column (user_one and user_two) and the query only returns rows that have no matching jointure.
Hope this will help you.
SELECT table1.id
FROM table1
LEFT JOIN table2 ON table1.id = table2.user_one
WHERE table2.user_one is NULL
Try following query:-
SELECT table1.id
FROM table1
where table1.id
NOT IN (SELECT user_one
FROM Table2
UNION
SELECT user_two
FROM Table2)
Hope this helps you.
Try:
SELECT A.id FROM
(
SELECT table1.id FROM table1
LEFT JOIN table2 ON table1.id = table2.user_one
WHERE table2.user_one IS NULL
) A
JOIN (
SELECT table1.id FROM table1
LEFT JOIN table2 ON table1.id = table2.user_two
WHERE table2.user_two IS NULL
) B
ON A.id = B.id
See Demo
Or you could use two LEFT JOINS with aliases like:
SELECT table1.id FROM table1
LEFT JOIN table2 A ON table1.id = A.user_one
LEFT JOIN table2 B ON table1.id = B.user_two
WHERE A.user_one IS NULL
AND B.user_two IS NULL
See 2nd Demo

SQL join ON not equal in Mysql

I have two tables. Both contains question id field. I want to get all records from first table that are not present in second one. I don't want to use "NOT IN" constrain as second table having more than 400000 records.
Try something like
SELECt t1.*
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.questionID = t2.questionID
WHERE t2.questionID IS NULL
Typically you would do this using a LEFT JOIN combined with a WHERE clause selecting every row where the joined table returns no results.
SELECT t1.*
FROM Table1 t1
LEFT OUTER JOIN Table2 t2 ON t2.ID = t1.ID
WHERE t2.ID IS NULL
try:
select from t1
right join t2 on t2.id = t1.id where t2.id is null