Arkadiusz
Arkadiusz

Reputation: 15

How to join one table two times, but get different column names?

Question:

How can I join one table two times (in this case sl_jednostka_miary), but get different column names without specifying every column? I could just call every column, but there is too many of them.

EDIT: I can call every column by name from sl_jednostka_miary, but I don't want to call columns from other tables.

What I tried:

SELECT *, j_porownawcza.jm_id AS jp_id, j_porownawcza.jm_kod AS jp_kod, j_porownawcza.jm_opis AS jp_opis
FROM tw__towar
        INNER JOIN sl_typ_towaru ON tt_id = tw_ttid
        INNER JOIN sl_jednostka_miary ON jm_id = tw_jmid
        INNER JOIN sl_jednostka_miary AS j_porownawcza ON j_porownawcza.jm_id = tw_jpid

Problem:

I've tried query above, but the problem is that I get this table shown three times - two times with the same column names, and one time with new column names.

Upvotes: 0

Views: 490

Answers (1)

Javad Ghasemi
Javad Ghasemi

Reputation: 221

You must specify an alias for each table and use [alias_name].* from list of all columns of each table.

Thy this:

Select W.*, S.* , t1.* 
-- Must list field name for only one table 
, t2.Col1 as t2_Col1, t2.Col2 as t2_Col2 , t2.Col3 as t2_Col3 ,....
FROM tw__towar AS W
        INNER JOIN sl_typ_towaru AS S  ON S.tt_id = W.tw_ttid
        INNER JOIN sl_jednostka_miary AS t1 ON t1.jm_id = W.tw_jmid
        INNER JOIN sl_jednostka_miary AS t2 ON t2.jm_id = W.tw_jpid

Upvotes: 4

Related Questions