SQL从一个表中选择两个与单个表相关的字段

时间:2013-11-07 15:16:27

标签: mysql sql select join

我有2张桌子

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

我想进行查询以同时获取libFrequenceDUREE_FREQ_ID的{​​{1}}!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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);