Query for changing the id to name from two different tables - sql

I have a table C which contains id of table A and also id of table B. How can I select all the unique rows and change the ids into corresponding names from table A and B.
table contain IDs Table A Table B
--------------- --------------- ---------------
A_id | B_id A_id | Name B_id | Name
--------------- --------------- ---------------
1 | 2 1 | AA 1 | a
1 | 4 2 | BB 2 | b
3 | 2 3 | CC 3 | c
3 | 1 4 | DD 4 | d
3 | 1
The result must be
---------------
A_Name | B_Name
--------------
AA | b
AA | d
CC | b
CC | a
Many Thanks for your help.

what you need is
INNER JOIN:The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables. and
DISTINCT:The DISTINCT keyword can be used to return only distinct (different) values.
select distinct a.name,b.name
from c
inner join b using(b_id)
inner join a using(a_id)
Go through PostgreSQL - JOINS for more info.

One approach is to join across all three tables:
SELECT DISTINCT A.name, B.name
FROM A
INNER JOIN C
ON A.A_id = C.A_id
INNER JOIN B
ON B.B_id = C.B_id
The 'DISTINCT' keyword eliminates duplicates (specifically, the last two rows in Table C).
This query works for me with your sample data.

Related

How to join two tables just by using where and select operators

What is the mysql command to join two columns where one table has the id and another table has the id and name by using where and select operators.
In table_a :
ID | NAME
1 | A
2 | B
3 | C
4 | D
table_b :
ID | AGE
1 | 60
2 | 50
3 | 40
4 | 30
the expected result,
ID | NAME | AGE
1 | A | 60
2 | B | 50
3 | C | 40
4 | D | 30
What I understood from the question that you have asked is that you want to merge two tables and get the expected result.
For ex: let's take 2 tables called table_a and table_b
In table_a :
ID | NAME
1 | A
2 | B
3 | C
4 | D
table_b :
ID | AGE
1 | 60
2 | 50
3 | 40
4 | 30
I would expect the result that you want is ,
ID | NAME | AGE
1 | A | 60
2 | B | 50
3 | C | 40
4 | D | 30
If so you can get the expected result by using the following sql command,
SELECT a.id,a.name,b.id,b.age
FROM `table_a ` a , `table_b ` b
WHERE a.id= b.id ;
With FROM table_a a , table_b b I'm assigning the 2 tables to a and b respectively so that it will make the query readable and more understandable.
But Note that its' always better to use JOIN syntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed.
Here are the different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both tables.
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table.
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table.
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table.
For further reference on joins you can use the following link. https://www.w3schools.com/sql/sql_join.asp
Hope this will help you to solve your issue.

Access SQL: Self join without cross duplicates

Table:
Bag | Weight
------------
A | 2
B | 2
C | 3
I want to know which bags have similar weights; normally I would self-join on the Weight column; but I don't want cross duplications; that is if I have A-B, I don't want B-A.
Query:
SELECT lhs.bag, rhs.bag
FROM myTable AS lhs INNER JOIN myTable AS rhs ON lhs.weight = rhs.weight;
Result:
lhs.bag | rhs.bag
-----------------
A | A
A | B
B | B
B | A
C | C
I don't want the row B|A in the results, because similarly it has been in A|B.
Expected Table:
lhs.bag | rhs.bag
-----------------
A | A
A | B
B | B
C | C
Extend the join condition
SELECT lhs.bag, rhs.bag
FROM myTable AS lhs
INNER JOIN myTable AS rhs ON lhs.weight = rhs.weight AND
lhs.bag <= rhs.bag;

Showing the full list IDs when OUTER JOIN two tables

How to display not only the overlapped IDs from two tables (table A and table B) but also the unique IDs from two tables?
Here is the example code:
SELECT A.ID AS ID
FROM A
FULL OUTER JOIN B ON A.ID = B.ID
I think there's something I can do with the SELECT line but I don't know how.
coalesce() returns the first non-null value from a set of parameters. Is that what you are looking for?
rextester demo: http://rextester.com/QYAZV8300
create table a (id int)
insert into a values (1),(3),(5)
create table b (id int)
insert into b values (2),(3),(4)
select
a.id as A_Id
, b.id as B_Id
, coalesce(a.id,b.id) as Id
from a
full join b
on a.id = b.id
returns:
+------+------+----+
| A_Id | B_Id | Id |
+------+------+----+
| 1 | NULL | 1 |
| 3 | 3 | 3 |
| 5 | NULL | 5 |
| NULL | 2 | 2 |
| NULL | 4 | 4 |
+------+------+----+

sql statement for multiple fk columns

I have two tables. The table A contains 3 columns referencing the id of table B. How do I write a select statement showing all names of table B referenced by table A.
Take a look it will explain itself pretty well:
table a is like this
----------------------
aid | bfk1 | bfk2 | bfk3
-----------------------
1 | 1 | 3 | 4
2 | 1 | 4 | 4
3 | 1 | 4 | 4
table b holds names
----------------------
bid | name
-----------------------
1 | test1
2 | test2
3 | test3
4 | test4
how to show all data from table a with names of table b. What sql statement do I need to achive this?
thanks in advance
You need to join the same table 3 times with different alias names
select a.aid,
b1.name as b_name_1,
b2.name as b_name_2,
b3.name as b_name_3
from tableA a
left join tableB b1 on a.bfk1 = b1.bid
left join tableB b2 on a.bfk2 = b2.bid
left join tableB b3 on a.bfk3 = b3.bid

SQL query with two columns as foreign keys of the same table

I have two tables
Table A
id ! name ! fk_1_table_B_1 ! fk_2_table_B_2
-------|------|----------------|--------------
1 | John | 1 | 3
2 | Paul | 2 | 1
3 | Anna | 4 | 2
4 | Alan ! 3 | 1
Table B
id | code
-------|------
1 | EN
2 | US
3 | FR
4 | IT
The idea is to obtain the following query
id ! name ! code (fk_1_table_B_1) ! code (fk_1_table_B_2)
-------!------!-----------------------!-----------------
1 | John | EN | FR
2 | Paul | US | EN
3 | Anna | IT | US
4 | Alan ! FR | EN
If Table A had only one FK Column from Table B I would do
SELECT tableA, name, tableB.code
FROM tableA, table B
WHERE tableA.fk_1_table_B_1 = tableB.id
How can I do this with Table A having two columns as FK from B? What should I select in the SELECT?EN
Thanks
You should join to the same table twice, giving it two different aliases:
SELECT a.id, a.name, b1.code, b2.code
FROM tableA a
JOIN tableB b1 ON b1.id = a.fk_1_table_B_1
JOIN tableB b2 ON b2.id = a.fk_2_table_B_2
Note how this query uses ANSI join syntax for better clarity: rather than listing all tables in the FROM clause, it puts each of the aliased tableBs in its own JOIN clause.
Maybe this?
select a.id, a.name,
(select b.code from B b where b.id = a.fk_1_table_B_1),
(select c.code from B c where c.id = a.fk_1_table_B_2),
from A a