Coder Passion
Coder Passion

Reputation: 65

SQL Select from one table twice field related to single table

I have 2 table

--------------------------------------------
TABLE 1:
ID
DUREE_FREQ_ID => foreign key idFrequence
POSOL_FREQ_ID => foreign key idFrequence
INTER
------------------------------------------
TABLE 2:
idFrequence
libFrequence
-------------------------------------------

I want to make a query to get libFrequence for DUREE_FREQ_ID and POSOL_FREQ_ID at the same time!

Upvotes: 0

Views: 83

Answers (2)

SQB
SQB

Reputation: 4078

SELECT
  t1.id,
  t1.duree_freq_id,
  td.libfrequence AS duree_freq,
  t1.posol_freq_id,
  tp.libfrequence AS posol_freq
FROM table1 t1
LEFT JOIN table2 td ON (td.idfrequence = t1.duree_freq_id)
LEFT JOIN table2 tp ON (tp.idfrequence = t1.posol_freq_id);

Upvotes: 0

Linger
Linger

Reputation: 15048

SELECT Table1.*, t2a.libFrequence AS DUREElib, t2b.libFrequence AS POSOLlib
FROM Table1
INNER JOIN Table2 AS t2a ON Table1.DUREE_FREQ_ID = t2a.idFrequence
INNER JOIN Table2 AS t2b ON Table1.POSOL_FREQ_ID = t2b.idFrequence

Upvotes: 1

Related Questions