Join two tables... without JOIN - mysql

I've got two tables T1 and T2, both with a single field (id).
T1.id has values:
1
2
4
T2.id has values:
1
3
4
I need to join these tables.
Desired result:
T1 | T2
------|------
1 | 1
2 | null
null | 3
4 | 4
With JOIN I'd do it easily:
Query 1
SELECT * FROM T1 FULL JOIN T2 ON T1.id=T2.id
But due to certain reasons I can't use JOIN here. So, with a simple query like this
Query 2
SELECT * FROM T1, T2 WHERE T1.id=T2.id
I would get only two rows of data
T1 | T2
------|------
1 | 1
4 | 4
as two other rows would be omitted due to no matches in the other table.
No matter what to fill the missing matches with. It could be NULL or any other value - really anything, but I need to get those omitted rows.
Is there a way to modify Query 2 to get the desired result without using any JOIN?
PS: Real tables are different in structure, so UNION is not allowed either.
PPS: I've just given a model to point out the problem. In reality it's a "megaquery" involving many tables each having dozens of columns.

Standard way to implement FULL OUTER JOIN when only implicit joins are supported.
select t1.id t1id, t2.id t2id
from t1, t2 where t1.id = t2.id
union all
select id, null from t1
where not exists (select 1 from t2 where t2.id = t1.id)
union all
select null, id from t2
where not exists (select 1 from t1 where t1.id = t2.id)
order by coalesce(t1id, t2id)
The first SELECT produces the INNER JOIN part of the result.
The second SELECT adds the additional LEFT OUTER JOIN rows to the result.
The third SELECT adds the additional RIGHT OUTER JOIN rows to the result.
All together, a FULL OUTER JOIN is performed!
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=ec154ad243efdff2162816205fdd42b5

SELECT t1.id t1_id, t2.id t2_id
FROM ( SELECT id FROM table1
UNION DISTINCT
SELECT id FROM table2 ) t0
NATURAL LEFT JOIN table1 t1
NATURAL LEFT JOIN table2 t2

Related

MySQL: Get data from multiple tables allowing nulls with a list of ids

I got this tables that look like this:
table 1
|id|value1|value2|value3
table 2
|id|value4|value5|value6
The id value in each table is unique but an id could appear in a table 1 but no in table 2. (value 1 is equal to value4 but if id dont appear in table 2 value4 would be null... )
Then I got this a of ids and I want to get sometime like (supossing that id appear in table 1 but no in 2 and vice versa):
resultgrid
| id | value1| value2| value3|value4|value5|value6
|838383|result1|result2|result3|null |null |null
|548438|null |null |null |result4|result5|result6
hope you guys can help me, thanks!
EDIT: query i've been trying (it's actually a set of collected pieces of answer i'd see in stack overflow)
SELECT t1.*, t2.value4, t2.value5, t2.value6
FROM table1 as t1
JOIN table2 AS t2 ON t2.id = t1.id
Where t1.id = t2.id = 838383
this get me 0 rows returned.
I want to make it general to use the <2000 id list.
You want a full outer join which MySQL does not support. In your case, you can emulate this with left join:
select t1.*, t2.value4, t2.value5, t2.value6
from (select 838383 as id
) i left join
table1 t1
on t1.id = i.id left join
table2 t2
on t2.id = i.id;
The list of ids you want to keep goes in the i subquery.
You can use two different Select queries, using Left join between the two tables. In the first query, consider table1 as leftmost table; and table2 as leftmost in the second query.
Use Where <right table id> IS NULL to filter out rows where there is no matching entry in the rightmost table.
Use Union to combine the resultset. Since there will not be any duplicates (due to our query results), we can use Union All.
Try the following:
SELECT t1.id, t1.value1, t1.value2, t1.value3,
t2.value4, t2.value5, t2.value6
FROM table1 AS t1
LEFT JOIN table2 AS t2 ON t2.id = t1.id
WHERE t2.id IS NULL
UNION ALL
SELECT t2.id, t1.value1, t1.value2, t1.value3,
t2.value4, t2.value5, t2.value6
FROM table2 AS t2
LEFT JOIN table1 AS t1 ON t1.id = t2.id
WHERE t1.id IS NULL

I have two tables,first table T1 contains id and name and the name contains 5 values

I have two tables,first table T1 contains id and name and the name contains 5 values.And the second table T2 contains id and amonut_paid and the amount paid column contains 3 values.I want to display the all names from the table T1 and the amount_paid in sql
Do a LEFT JOIN and then use IFNULL for replacing the NULL with 0
SELECT T1.name,IFNULL(T2.amount_paid,0)
FROM T1
LEFT JOIN T2 ON T1.id=T2.id
Note: Instead of IFNULL you can also use COALESCE as strawberry suggested.
Use left join here, Because your t1 table contains 5 names and you want to display all name from t1 table. And your t2 table contains only three entry(amount_paid).
Select t1.name, t2.amount_paid
from t1
left join t2 on t1.id = t2.id
Use an LEFT JOIN on both these tables T1 and T2, so that it shows only the rows where a NAME against AMOUNT_PAID exists (INNER JOIN provides the rows available in both these tables)
SELECT T1.name, T2.amount_paid
FROM T1
LEFT JOIN T2 ON T1.id = T2.id
--RETURNS 3 rows as per your example
Use an OUTER JOIN on both these tables T1 and T2, so that it shows all the rows irrespective of the values being present in these tables.
RETURNS 5 rows as per your example

how to make query show the an other column of the primary key?

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]

mysql join where the difference between values is minimal?

In principle I have 2 tables T1 and T2, each containing a date-field, lets call it date:
T1: date | somekey | data ...
T2: date | somekey | data ...
I want to (left)join T1 and T2, such that the result has all rows from T1.
Now for each row from T1 I need the values from the (exactly one) row of T2 joined where ABS(DATEDIFF(T1.date,T2.date)) is minimal and T1.somekey=T2.somekey (or null in case there is no such row in T2)
Don't do it in the JOIN clause, just put it in the WHERE clause.
SELECT
*
FROM
T1
LEFT JOIN T2 ON T1.somekey = T2.somekey
WHERE
ABS(DATEDIFF(T1.date,T2.date)) = 1 /*or whatever "minimal" means for you*/
/*or like this:*/ < $yourMinimalValue
EDIT:
I think I get what you want to do, but subqueries are the way to go here:
SELECT
*
FROM
T1
LEFT JOIN T2 ON T1.somekey = T2.somekey
WHERE
ABS(DATEDIFF(T1.date,T2.date)) = (SELECT MIN(DATEDIFF(TT1.date, TT2.date)) FROM T1 AS TT1 LEFT JOIN T2 AS TT2 ON TT1.somekey = TT2.somekey)

Mysql Query optimization

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