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)
Related
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
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 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid
If I have two tables that I'm joining and I write the most simple query possible like this:
SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
There are a few records who have multiple rows per ID because they have multiple employers, so t1 looks like this:
ID Name Employer
12345 Jerry Comedy Cellar
12345 Jerry NBC
12348 Elaine Pendant Publishing
12346 George Real Estate
12346 George Yankees
12346 George NBC
12347 Kramer Kramerica Industries
t2 is linked with the similar IDs but with some activities that I'd like to see -- hence the SELECT * above. Though I don't want multiple rows to return if the Employer column is "NBC" -- but everything else is good.
The only other thing that matters here is that t2 is smaller than t1, because t1 is everybody and t2 are only from people who did particular activities -- so some of the matches won't return anything from t2, but I would still like them to be returned, hence the LEFT JOIN.
If I write the query like this:
SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
WHERE Employer <> "NBC"
Then it removes Jerry and George completely -- when really all I want is for the NBC row to not be returned, but to return any other rows that are associated with them.
How can I write the query while joining t1 with t2 to return each row except for the NBC ones? The ideal output would be all of the rows from t1 regardless if they match up with all of t2 except removing all of the rows with "NBC" as the employer in the return file. Basically the ideal here is to return the JOINs where they fit, but regardless remove the entire row for anybody with "NBC" as employer without removing their other rows.
The more I write about it, it seems like I should potentially just run a query prior to my JOIN to delete all the rows in t1 who have "NBC" as their employer and then run the normal query.
Basic subset filtering
You can filter either of the two merged (joined) subsets by extending the ON clause.
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer != 'NBC'
If you get null values now, and you don't want them, you'd add:
WHERE t2.Employer IS NOT NULL
extended logic:
SELECT *
FROM t1
LEFT JOIN t2
ON (t1.ID = t2.ID AND t2.Employer != 'NBC')
OR (t2.ID = t2.ID AND t2.Employer IS NULL)
Using UNION
Basically, JOIN is for horizontal linking and UNION does vertical linking of datasets.
It merges to resultsets: the first without NBC, and the second (which is basically an OUTER JOIN), adds everyone in t1 which is not part of t2.
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer != 'NBC'
UNION
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer IS NULL
String manipulation in the resultset
If you just want to remove NBC as a string, here is a workaround:
SELECT
t1.*,
IF (t2.Employer = 'NBC', NULL, t2.Employer) AS Employer
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
This basically replaces "NBC" by NULL
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]