I have tables_1 and table_2
table_1
id name cost
100 joe 10
101 bob 20
102 mary 30
table_2
id name
100 joe
101 bob
102 mary
103 tom
I want to join these tables even no match id and expect result
id name cost
100 joe 10
101 bob 20
102 mary 30
103 tom null
My query
select t1.id, t1.name, t1.column1, t1.column2
from Table_1 t1
join Table_1 t2 on t1.id = t2.id
and t1.id <> t2.id
I got nothing return. Need some help. Thank you
It looks like you need a simple outer join
select t2.id, t2.name, t1.cost
from table_2 t2
left join table_1 t1 on t1.id=t2.id
What you need is a right join (or a left join if you reverse the order of tables in the join)
select table_2.id, table_2.name, table_1.cost
from table_1 right join table_2
on table_1.id = table_2.id
Related
T1 - table one with only one column
Bart
Philip
Beth
T2 - table two with only one column
Robert
Bart
Philip
Ann
Jack
Helen
Beth
The expected result after JOIN is:
Robert
Ann
Jack
Helen
What you are after is a simple outer join
select t2.col
from t2
left join t1 on t2.col=t1.col
where t1.col is null
You can also express it as a not exists
select *
from t2
where not exists(select 1 from t1 where t2.col=t1.col)
You can also express it using except
select *
from t2
except
select *
from t1
I have two tables, t1 and t2.
Table t1:
Name address id
---- ------- --
rob 32 cgr 12
mary 31 lmo 42
tom axel St 2
Table t2:
ID Flag expense
-- ---- --------
12 Shop 1200
12 Educ 14000
42 educ 4000
Now I will have to create a table which will have attributes from t1 plus two more attributes that is expense in shop and expense in educ
Table t3
Name address id Shop_ex Educ_ex
---- ------- -- ------- -------
rob 32 cgr 12 1200 14000
mary 31 lmo 42 NULL 4000
tom axel st 2 NULL NULL
How to accomplish this?
I tried doing a left join t2 with switch case but it gives me multiple record as the join is becoming one to many.
select
t1.name, t1.address, t1.id,
case
when t2.flag = "shop" then t2.expense
else null
end as shop_ex
case
when t2.flag = "educ" then t2.expense
else null
end as educ_ex
from
t1
left join
t2 on (t1.id = t2.id)
It seems I will have to convert t2 table first before joining, to have a single record on the basis of flag. But I am not sure how to do that.
Please mind the tables are huge and optimized query will be nice.
Please suggest.
You only need to join the first table to the second one, twice:
SELECT t1.Name, t1.address, t1.id, t2a.expense AS Shop_ex, t2b.expense AS Educ_ex
FROM table1 t1
LEFT JOIN table2 t2a
ON t2a.ID = t1.id AND t2a.Flag = 'Shop'
LEFT JOIN table2 t2b
ON t2b.ID = t1.id AND t2b.Flag = 'Educ'
Demo
Hard to make a good title for this (feel free to edit), but hope it will make more sense ..
Say I have the following tables:
t1:
i1 v1
_________
1 bob
2 NULL
3 sam
4 NULL
5 kenny
5 NULL
t2:
i2 v2 item
______________
1 bob prod_1
2 nick prod_2
3 sam prod_3
4 jj prod_4
5 kenny prod_5
5 cartman prod_6
I need to JOIN the tables on t2.i2 = t1.i1 but only where t2.v2 does not exist in t1.v1. So I'm trying to get the following results:
Goal:
i2 v2 item
__________________
2 nick prod_2
4 jj prod_4
5 cartman prod_6
This query below was my first attempt, and it's not working, so I'm trying to find a working and more efficient solution with JOINs.
SELECT * FROM t2
WHERE v2 NOT IN (
SELECT v1 FROM t1 WHERE t2.i2 = t1.i1
)
Your query is fine, although I would use NOT EXISTS:
SELECT dm2.*
FROM web.delete_me2 dm2
WHERE NOT EXISTS (SELECT 1
FROM web.delete_me1 dm1
WHERE dm2.some_int2 = dm1.some_int1 and dm2.some_var2 = dm1.some_var1
);
Although you can write this as a join, this version should be at least as good performance wise. You want an index on web.delete_me1(some_int1, some_var1).
A LEFT JOIN should be enough
select * From t2
left join t1 on t2.i2 = t1.i1 and t1.v1= t2.v2
where t1.i1 is null
SELECT DISTINCT T1.*, T2.*
FROM T1
JOIN T2
ON T1.ID = T2.ID
LEFT JOIN T2 AS T2NO
ON T2NO.NAME = T1.NAME
WHERE T2NO.NAME IS NULL
or try this
SELECT DISTINCT T2.*
FROM T2
JOIN T1
ON T1.ID = T2.ID
LEFT JOIN T1 AS T1NO
ON T1NO.NAME = T2.NAME
WHERE T1NO.NAME IS NULL
Do you need all records from t2 where i2 is in t1 and v2 is not?
If so, you can write this query
select * from t2
where i2 in (select i1 from t1)
and v2 not in (select v1 from t1 where v1 is not null)
for not in to work as we need, v1 shouldn't be null
in my database i have 2 tables.
table1
i have ID and NAMES
table2
i have ID, IDASSOCIATION, QUANTITY
so
i have 2 names in table1:
john and tom
and in table2 i have 3 lignes
john, 1
tom, 1
john, 1
nombre one is the quantity
in my result i want get
john = 2
and tom = 1
so i do this:
sql = "SELECT t1.*, t2.IDASSOCIATION, (SELECT SUM(t2.id_qte) FROM associationdepotarticle t2 WHERE t1.fusiontable = t2.fusiontable GROUP BY t2.IDASSOCIATION) as id_qte FROM articletable t1, associationdepotarticle t2";
but i not get this:
john = 2
tom = 1
why ? what i will do, i need correction please
You can just join the tables together and use sum:
select t1.name, sum(t2.quantity)
from table1 t1
join table2 t2 on t1.id = t2.idassociation
group by t1.name
It's not completely clear from your sample data what to join on, but I assume it's the idassociation field. If you want to return those names in table1 which aren't in table2, then use an outer join.
I have one complicated question. I'll try to explain it with example:
have one table that have primary key, and I want to join other table there the first's table primary key is foreign key, and I want If in the second table there is duplicate foreign key to select the number of repeatability. For example:
1-st table:
id name
--- -----
1 Greg
2 Alan
3 George
4 John
5 Peter
2-nd table
id aid data
--- ----- -------
1 2 CCCV
2 2 VVVV
3 3 DDDDD
4 3 SSSS
5 4 PPPPP
I want the result of the join to be:
id(1st table) aid name Data Number
----------- ---- ----- ----- -----
1 null Greg null 1
2 1 Alan CCCV 1
2 2 Alan VVVV 2
3 3 George DDDDD 1
3 4 George SSSS 2
4 5 John PPPPP 1
5 null Peter null 1
I searched a lot, I couldn't find anything. Maybe I do not know how search, or there is no such thing as what I want to do.
SELECT Table1.id, Table2.id as aid, Table1.name, Table2.data,
GREATEST(1, (SELECT COUNT(*)
FROM Table2 t2
WHERE t2.aid = Table1.id
AND t2.id <= Table2.id))
AS number
FROM Table1
LEFT JOIN Table2
ON Table2.aid = Table1.id
ORDER BY id, aid;
works in both MySQL and PostgreSQL.
As per my comment, you've tagged this both MySQL and PostgreSQL.
This answer is for PostgreSQL.
SELECT
table1.id,
table2.aid,
table1.name,
table2.data,
ROW_NUMBER() OVER (PARTITION BY table1.id ORDER BY table2.aid) AS number
FROM
table1
LEFT JOIN
table2
ON table1.id = table2.aid
Queries for PostgreSQL 8.3 which has no window functions.
With bigger tables it is regularly much faster to use a JOIN instead of a correlated sub-query.
The first query aggregates values for Table2 before joining to Table1, which should befaster, too:
SELECT t1.id, t2.aid, t1.name, t2.data, COALESCE(t2.ct, 1) AS number
FROM Table1 t1
LEFT JOIN (
SELECT x.aid, x.data, count(y.aid) + 1 AS ct
FROM Table2 x
LEFT JOIN Table2 y ON x.aid = y.aid AND x.id > y.id
GROUP BY x.aid, x.data
) t2 ON t2.aid = t1.id
ORDER BY t1.id, t2.ct;
And ORDER BY should be fixed.
Alternative without sub-query. Might be faster, yet:
SELECT t1.id, t2.aid, t1.name, t2.data, count(*) + count(t3.id) AS number
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.aid = t1.id
LEFT JOIN Table2 t3 ON t3.aid = t2.aid AND t3.id < t2.id
GROUP BY t1.id, t2.aid, t1.name, t2.data
ORDER BY t1.id, count(t3.id);
Not sure, didn't test with a bigger set. Test performance with EXPLAIN ANALYZE. Could you report back your results?