How to Join Multiple SQL Tables Using the Ids

How can I join multiple SQL tables using the IDs?

You want something more like this:

SELECT TableA.*, TableB.*, TableC.*, TableD.*
FROM TableA
JOIN TableB
ON TableB.aID = TableA.aID
JOIN TableC
ON TableC.cID = TableB.cID
JOIN TableD
ON TableD.dID = TableA.dID
WHERE DATE(TableC.date)=date(now())

In your example, you are not actually including TableD. All you have to do is perform another join just like you have done before.

A note: you will notice that I removed many of your parentheses, as they really are not necessary in most of the cases you had them, and only add confusion when trying to read the code. Proper nesting is the best way to make your code readable and separated out.

How to join the same table with multiple ids?

Joining the same table twice should work just fine.

You haven't posted the entire table structure but shouldn't you be joining the kategorien table on the Id-column?
Like this:

SELECT wochenplan_id, wochentag_id, gerichte.gericht, gerichte.kalorien, preise.preis, kat1.kategorie, kat2.kategorie, gerichte.inhaltsstoffe 
FROM wochenplan
LEFT JOIN gerichte ON wochenplan.gericht_id = gerichte.gericht_id
LEFT JOIN preise ON gerichte.preis_id = preise.preis_id
LEFT JOIN kategorien AS kat1 ON gerichte.kategorie_id = kat1.kategorie_id
LEFT JOIN kategorien AS kat2 ON gerichte.kategorie2_id = kat2.kategorie_id
WHERE wochenplan.kw_jahr = 102021;

Joining and combining two tables that have the same ID for different rows

You have to use UNION, not JOIN for your purposes like this:

CREATE TABLE tab_a (
id int PRIMARY KEY,
utc text,
name text
);
CREATE TABLE tab_b (
id int PRIMARY KEY,
utc text,
name text
);

INSERT INTO tab_a (id, utc, name)
VALUES (1, "utc1", "name1"), (2, "utc2", "name2");

INSERT INTO tab_b (id, utc, name)
VALUES (1, "utc3", "name3"), (2, "utc4", "name4");

SELECT id, name, utc, "tab_a" as tab from tab_a
UNION SELECT id, name, utc, "tab_b" as tab from tab_b;

Response:

idnameutctab
1name1utc1tab_a
1name3utc3tab_b
2name2utc2tab_a
2name4utc4tab_b

How to JOIN Multiple Tables in an SQL Database in Order to Get Product IDs, OrderIDs, and the Customer info all in One Table?

Start with inner joins. Left join means you show the row on the left even if the row on the right isn’t there. Time for left joins after you can do inner joins.

It helps to have an ERD. If you don’t have one already, draw one. Pick a starting table that looks like a good place to get all the information you need. The joins follow the relationships in the diagram.

I would first join order to order detail using order Id.
Then join order detail to product using product Id.
Then join order to customer by customer Id.

Add the joins one at a time and confirm the results make sense. Notice that if you join orders to order details by order Id, you are going to get back a number of rows equal to the number of details, not the number of orders.

Joining multiple tables with different ID's

Simply add another join clause:

SELECT ...
FROM TABLE_ORDERS_PRODUCTS op
LEFT JOIN TABLE_PRODUCTS p ON op.products_id = p.products_id
LEFT JOIN TABLE_ORDERS_PRODUCTS_ATTRIBUTES pa ON op.products_id = pa.orders_products_id
WHERE ...

MySQL Join Multiple IDs From Another Table

you need several joins, one for each fieldID

select b.Name, a.Field1Data, c.name, a.Field2Data, d.name, a.FieldeData
from table2 a
inner join table1 b on b.id = a.Field1ID
inner join table1 c on c.id = a.Field2ID
inner join table1 d on d.id = a.Field3ID

Joining Multiple Tables with different structures?

There are a couple of ways to achieve this, but perhaps the simplest is as Dale K suggested to PIVOT on tablec before joining to it. To give you an idea how this would work, something along the lines of:

declare @main_table table (id varchar(5), name varchar(5));
declare @tablea table(id varchar(5), val varchar(5));
declare @tableb table(id varchar(5), val varchar(5));
declare @tablec table(id varchar(5), val varchar(5), idx int);

INSERT INTO @main_table
VALUES
('01','bb'),
('02','cc'),
('03','dd'),
('04','ff'),
('05','gg');

INSERT INTO @tablea
VALUES
('01','ab'),
('03','ac'),
('05','ad');

INSERT INTO @tableb
VALUES
('01','ba'),
('02','bc'),
('04','bd');

INSERT INTO @tablec
VALUES
('01','cc',1),
('01','cdf',2),
('01','cba',3),
('03','ggg',1),
('03','dfg',2);


with cte as (
SELECT id, [1], [2], [3], [4]
FROM
@tablec c
PIVOT
(
min(val)
FOR idx
IN ([1], [2], [3], [4])
) pvt
)
SELECT m.id, m.name, a.val AS val_TA, b.val AS val_TB,
c.[1] AS val_TC_index1, c.[2] AS val_TC_index2,
c.[3] AS val_TC_index3, c.[4] AS val_TC_index4
FROM @main_table m
LEFT JOIN @tablea a on a.id = m.id
LEFT JOIN @tableb b on b.id = m.id
LEFT JOIN cte c on c.id = m.id;

I have given a maximum of 4 index columns, but it is easy to see how to extend. If you need this to be open ended, then you will need to convert this into dynamic SQL.

Please note (because you are relatively new) that I have done the hard work for you (providing statements to create the tables and insert the data). In the future when asking such questions, please either do something similar or use db fiddle. You will get many more people willing to help you, if they don't have to do so much typing. Please remember that people are giving you their time for free.



Related Topics



Leave a reply



Submit