2010-08-06 91 views
0

我有一個SQL語法問題。SQL語法連接表

這裏是我的表:

default_caps: 
+-------+------------+------+-----+---------+----------------+ 
| Field | Type  | Null | Key | Default | Extra   | 
+-------+------------+------+-----+---------+----------------+ 
| idx | int(11) | NO | PRI | NULL | auto_increment | 
| tab | int(11) | NO |  | NULL |    | 
| cap | tinyint(1) | NO |  | 0  |    | 
| value | double  | YES |  | NULL |    | 
+-------+------------+------+-----+---------+----------------+ 

convertEntries: 
+----------------+------------+------+-----+---------+----------------+ 
| Field   | Type  | Null | Key | Default | Extra   | 
+----------------+------------+------+-----+---------+----------------+ 
| idx   | int(11) | NO | PRI | NULL | auto_increment | 
| convertTable | int(3)  | NO |  | 0  |    | 
| convertEntry | int(2)  | NO |  | 0  |    | 
| cap   | tinyint(1) | NO |  | 1  |    | 
| capTable  | int(2)  | NO |  | 1  |    | 
+----------------+------------+------+-----+---------+----------------+ 

我想選擇convertEntries行,結果中的每一行中,我想包括default_caps.value這樣default_caps.tab等於convertEntries.capTable,和default_caps.cap等於convertEntries.cap。

所以,如果default_caps看起來是這樣的:

+-----+-----+-----+---------+ 
| idx | tab | cap | value | 
+-----+-----+-----+---------+ 
| 1 | 1 | 0 | 8650.75 | 
| 2 | 1 | 1 |  50 | 
| 3 | 1 | 7 |  350 | 
| 2 | 2 | 0 | 9000 | 
| 2 | 2 | 1 |  100 | 
| 3 | 2 | 7 |  200 | 
+-----+-----+-----+---------+ 

而且convertEntries看起來像這樣(省略了一些列):

+-----+----------+-----+ 
| idx | capTable | cap | 
+-----+----------+-----+ 
| 1 |  1 | 1 | 
| 2 |  1 | 1 | 
| 3 |  1 | 1 | 
| 4 |  1 | 1 | 
| 5 |  1 | 1 | 
| 6 |  1 | 1 | 
| 7 |  1 | 7 | 
| 8 |  1 | 7 | 
| 9 |  1 | 7 | 
| 10 |  1 | 0 | 
| 11 |  1 | 0 | 
| 12 |  1 | 0 | 
+-----+----------+-----+ 

我想這樣的結果:

+-----+----------+-----+---------+ 
| idx | capTable | cap | value | 
+-----+----------+-----+---------+ 
| 1 |  1 | 1 |  50 | 
| 2 |  1 | 1 |  50 | 
| 3 |  1 | 1 |  50 | 
| 4 |  1 | 1 |  50 | 
| 5 |  1 | 1 |  50 | 
| 6 |  1 | 1 |  50 | 
| 7 |  1 | 7 |  350 | 
| 8 |  1 | 7 |  350 | 
| 9 |  1 | 7 |  350 | 
| 10 |  1 | 0 | 8650.75 | 
| 11 |  1 | 0 | 8650.75 | 
| 12 |  1 | 0 | 8650.75 | 
+-----+----------+-----+---------+ 

謝謝!

回答

3
SELECT convertEntries.idx, convertEntries.capTable, convertEntries.cap, 
    default_caps.value 
FROM convertEntries 
LEFT JOIN default_caps 
    ON convertEntries.cap = default_caps.cap 
    AND convertEntries.capTable = default_caps.tab 
-1
select 
    a.idx, 
    a.cap, 
    a.capTable, 
    b.Value 
from convertEntries a 
inner join default_caps b on a.tab = b.captable 
          and a.cap = b.cap 
+1

內連接是一個壞主意,在這裏,因爲他不會沒有在默認表中的相應值獲得的行。 – JNK 2010-08-06 19:44:55

1

這是一個簡單的加入,依我看

From ConvertEntries e 
    Left Join default_caps c on e.tab=c.captable and e.cap=c.cap 

我做它作爲一個左連接,因爲架構很可能有數據的問題。如果您內部連接並且default_caps中不存在適當的查找,則該記錄將被忽略。

0

我會建議使用更有意義的列名,但是:

SELECT convertEntries.idx 
    , convertEntries.capTable 
    , convertEntries.cap 
    , default_caps.value 
FROM convertEntries 
    INNER JOIN default_caps 
    ON default_caps.tab = convertEntries.capTable 
     AND default_caps.cap = convertEntries.cap