我有一個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 |
+-----+----------+-----+---------+
謝謝!
內連接是一個壞主意,在這裏,因爲他不會沒有在默認表中的相應值獲得的行。 – JNK 2010-08-06 19:44:55