2012-02-08 63 views
0

選擇,我有以下數據結構:SQL兩個表

TABLE requirements 
ID 1, AGE 1, GENDER 1, USERID 1 
ID 2, AGE 1, GENDER 2, USERID 2 
ID 3, AGE 1, GENDER 2, USERID 3 


TABLE offers 
IDoffers 1, AGE 1, GENDER 2, USERID 1 
IDoffers 2, AGE 1, GENDER 1, USERID 2 
IDoffers 3, AGE 1, GENDER 1, USERID 3 

我需要選擇表的行要求 - 這條線用於從表中選擇數據的提供參數。在週期中。

SELECT * FROM報價WHERE(ID1 ... IDX從需求)

我需要這樣的結果:

FOR ID 1 FROM TABLE requirements - RESULT FROM offers: 
IDoffers 2, AGE 1, GENDER 1, USERID 2 
IDoffers 3, AGE 1, GENDER 1, USERID 3 

FOR ID 2 FROM TABLE requirements - RESULT FROM offers: 
IDoffers 1, AGE 1, GENDER 2, USERID 1 

FOR ID 3 FROM TABLE requirements - RESULT FROM offers: 
IDoffers 1, AGE 1, GENDER 2, USERID 1 

回答

3
SELECT 
    rquirements.ID, 
    offers.* 
FROM 
    requirements 
    INNER JOIN offers ON requirements.AGE=offers.AGE 
    AND requirements.GENDER=offers.GENDER 

循環通過所有要求。如果您想要特殊要求,請添加

WHERE rquirements.ID=<whatever>