I have 2 tables with ID.
I want to do something like a left join:
I want that if the ID from TABLE1 is not exist in TABLE2, it will search for ID in TABLE2 with the same 5 digits.
But I want it to search a match between last 5 digits just after it didn't find a completed match.
For example:
Table1:
ID Name
1111111 'aa'
2222222 'bb'
3333333 'cc'
4444444 'dd'
5555555 'ee'
6666666 'ff'
Table2:
ID City
1166666 're'
7833333 'tv'
4444444 'gh'
8547555 'ie'
6666666 'rt'
The accepted Results:
ID Name City Status
1166666 'aa' 're' ok
2222222 'bb' no_record
3333333 'cc' 'tv' ok \\last 5 digits are match.
4444444 'dd' 'gh' ok
5555555 'ee' no_record \\ just the last 3 digits are match.
6666666 'ff' 'rt' ok
In my example, if it will search together for a complete match or last 5 digits, he will match 6666666 to 1166666 and not to 6666666.
How should I write this?
In MS Access, you can use the following if the last five characters are unique:
select t1.*, t2.city,
iif(t2.id is not null, "ok", "no record)
from table1 as t1 left join
table2 as t2
on right(t2.id, 5) = right(t1.id, 5);
You can extend this for your purposes as:
select t1.*, nz(t2.city, t2_5.city) as city,
iif(t2.id is not null or t2_5.id is not null, "ok", "no record)
from (table1 as t1 left join
table2 as t2
on t2.id = t1.id
) left join
table2 as t2_5
on right(t2.id, 5) = right(t1.id, 5) and t2.id is null
you can get the last 5 of a number with MOD so the query would look like this:
SELECT T1.NAME, COALESCE(T2.CITY, T3,CITY) AS CITY
FROM TABLE1 T1
LEFT JOIN TABLE2 T2 ON T1.ID = T2.ID
LEFT JOIN TABLE2 T3 ON MOD(T1.ID,100000) = MOD(T2.ID,100000)
Join to table 2 twice. once on full ID, once on right ID and coalesce.
SELECT T1.ID, T1.Name, Nz(T2.City,T3.City) as City
FROM Table1
LEFT JOIN Table2 T2
on T1.ID = T2.ID
LEFT JOIN table2 T3
on right(T1.ID,5) = right(T3.ID,5)
if access doesn't have coalesce use if or isnull or NZ
Related
Hi I am new to mysql I have two tables I wanted to remove part of some values in column table1 that is found in table2 which is status is inactive
My query is like this for table1
Select list_name from table1;
Result
list_name
anna;carol;jess;lina;elsa
but I want to remove carol and elsa because in table2 they are inactive
table 2 query
select name,status from table2
name status
anna active
carol inaactive
jess active
lina active
elsa inactive
my expected output is
name
anna;jess;lina;
Here's your query
select replace(GROUP_CONCAT(t1.list_name ), ',', ';') from table1 t1
inner join table2 t2 on t2.name=t1.list_name
where t2.status = 'active'
You must to use a INNER JOIN statement:
SELECT
t1.list_name
FROM table1 AS t1
INNER JOIN table2 AS t2
ON t1.list_name = t2.name
WHERE t2.status <> 'inactive'
So that you will filter by status.
I have 2 tables like below
Table 1
ID Title Number
001 Student 10
002 Student 20
004 Teacher 25
Table 2
ID Title Number
001 Researcher 10
002 Student 20
004 Professor 25
I want to merge the 2 tables such that the number of the same ID are added together but the Title follows that of table 1, so an output like
ID Title Number
001 Student 20
002 Student 40
004 Teacher 50
Thanks.
Try This.
SELECT t1.ID, t1.Title, (t1.number + t2.number) as total
FROM table1 t1 JOIN table2 t2
ON t1.ID= t2.ID
One approach is to begin by doing a UNION ALL of the two tables, and computing the sum of the Number field, grouping on the ID. This result can then be joined back to Table 1 again to use the Title field there.
SELECT t2.ID, t1.Title, t2.Number
FROM
Table1 t1
INNER JOIN
(
SELECT t.ID, SUM(t.Number) AS Number
(
SELECT t1.ID, t1.Number
FROM Table1 t1
UNION ALL
SELECT t2.ID, t2.Number
FROM Table2 t2
) t
GROUP BY t.ID
) t2
ON t1.ID = t2.ID
select a.id,a.title,(a.number+b.number) as number from table1 as a INNER JOIN table2 as b on a.id=b.id;
I am very new in sql, then i am so confused how to get join or get value from two.
First table:
ID P_ID Name AGE U_ID
1 5 B 8 5w
2 8 D 17 6j
3 7 R 67 0qw
Second Table:
ID P_ID Address Edu
1 6 Bddd +2
2 7 Dssss Bachelor
3 2 rress Phd
Here, i want to get accorading to P_ID, but i have U_ID only.
For this: Let us assume that now I have U_ID=0qw.
How to get value from second table. Address and edu , and Age Thanks in advance.
Join on the column that both tables have in common.
select t1.age, t2.address, t2.edu
from table1 t1
join table2 t2 on t1.p_id = t2.p_id
where t1.u_id = '0qw'
Then use the table names or alias names (like t1 for table1) to pick columns from the tables you join.
I think you are looking forward to this:
SELECT t2.Address, t2.Edu, t1.Age
FROM firstTable t1
JOIN secondTable t2
ON t1.P_ID = t2.P_ID
WHERE t1.U_ID = '0qw'
SELECT table1.AGE
, table2.Address
, table2.Edu
FROM table1
INNER JOIN table2 ON (table1.P_ID = table2.P_ID)
WHERE table1.U_ID = '0qw';
NOTE: SQL query is not case sensitive.
I have 2 tables, let's say they contain:
Table1
id | username
1 | Bla
4 | Bla2
Table2
FROM_ID | FK_TOID | Action
1 | 2 | -1
4 | 2 | -1
Now all I have is something like:
SELECT FK_FROMID FROM table2 WHERE FK_TOID = 2 AND FP_ACTION = -1
What I want to do is fetch the id and username from table1, where the id of table1 matches FROM_ID from table2, where FK_TOID is '2' in table2.
So basically the results returned should be something like
[{ id: 1, FK_FROMID: 1, username: Bla }, { id: 4, FK_FROMID: 4, username: Bla2 }]
you need this:
SELECT A.id,B.from_id as FK_FROMID,A.username
FROM t1 A
LEFT JOIN t2 B
ON A.id=B.from_id
click this link to see the result:
http://sqlfiddle.com/#!2/868c1/4
update:1
SELECT A.id,B.from_id as FK_FROMID,A.username
FROM t1 A
LEFT JOIN t2 B
ON A.id=B.from_id
WHERE B.fk_toid=2 AND B.action=-1;
check this link:
http://sqlfiddle.com/#!2/868c1/8
try this:
SELECT * FROM table1
LEFT JOIN table2
ON table1.id = table2.from_id
where table2.TOID = '2'
You really don't need to use any joins for this. A simple query will work as follows:
select t1.id, t2.FK_FROMID, t1.username
from Table1 t1, Table2 t2
where t1.id = t2.FK_FROMID
Hope that helps!
You should use the inner join or left outer join (does not matter for your example) as follows:
select t1.id, t2.FK_FROMID, t1.username
from Table1 t1
join Table2 t2
on (t2.FROM_ID = t1.id)
where t2.FK_VOID = 2
and t2.FP_ACTION = -1
Lets say I have two tables:
Table1
Id Name
1 Joe
2 Greg
3 Susan
4 Max
Table2
Uid comment
2 Good customer
4 Great guy
What I want to do is list all elements of Table 1, and if Table1.Id = Table2.Uid I want to select this comment. If comment does not exist give blank field.
Result should be:
1 Joe
2 Greg Good customer
3 Susan
4 Max Great Guy
I can't figure out how to do it, if I write:
select
table1.Id,
table1.Name,
table2.comment
where
table1.id=table2.Uid
It gives me only users 2 and 4.
Try to use left join it shows you all data from table1
select t1.Id, t1.Name, t2.comment
from table1 t1
left join table2 t2 on t1.id=t2.Uid
NOTE:
Good practice is to use aliases as above. Code is more readable.
select
table1.Id,
table1.Name,
table2.comment
from table1 left outer join table2 on table1.id=table2.Uid
It is a classical JOIN operation:
SELECT
t1.id, t1.name, t2.comment
FROM
Table1 AS t1
LEFT JOIN
Table2 AS t2 ON t1.id = t2.uid