sql statement for multiple fk columns - sql

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

Related

how to Insert a key data and information of the table where it belong to, So that it can be searched using the key data in that table. in sql server

I have Multiple table that contains rows in it.
I have to make a table that contains id of a particular row, Tag Name and from which table that id belongs to so that if I want to search a tag I will get results of rows present in the multiple tables.
eg:
table 1
id | name | column 1
1 | A | ..
2 | B | ..
3 | C | ..
table 2
id | name | column 1 | column 2
1 | D | .. | ..
2 | E | .. | ..
3 | F | .. | ..
table 3
id | name
1 | G
2 | H
3 | I
above tables are those multiple tables that contains data in it.
Tag Table
Student Id | Tags | From which table it belongs to
1 | Good Boy | Table 1
1 | Good Boy | Table 2
3 | Good Boy | Table 1
2 | Bad Boy | Table 1
1 | Bad Boy | Table 3
If I want to retrieve data of good boys.
Expected Output
Id | name
1 | A
1 | D
3 | C
Please tell me a way to insert data like this so that if I can search data of good boys or bad boys.
Ideally, you would store the data from all three tables in a single table, with another column for BelongsTo.
You can manufacture this by unioning all the tables together, then you simply join on the result.
SELECT
t.id,
t.Name
FROM Tag
JOIN (
SELECT t.id, t.Name, 'Table1' BelongsTo
FROM table1 t
UNION ALL
SELECT t.id, t.Name, 'Table2'
FROM table2 t
UNION ALL
SELECT t.id, t.Name, 'Table3'
FROM table3 t
) t ON t.id = Tag.StudentId AND Tag.BelongsTo = t.BelongsTo
WHERE Tag.Tags = 'Good Boy';
db<>fiddle

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.

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 |
+------+------+----+

Query for changing the id to name from two different tables

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.

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