2015-08-24 205 views
0

我有一條語句連接兩個表。但是,我需要WHERE子句來匹配第二個表中的兩行。這裏有兩個表:加入2個表SQL - Where子句2在一個表中行

b_iblock_element 

ID | IBLOCK_ID | ACTIVE | ACTIVE_FROM | ACTIVE_TO 
1 | 3   | Y  | 15/09/2015 | 22/09/2015 
2 | 3   | Y  | 11/09/2015 | 20/09/2015 

b_iblock_element_property 

ID | IBLOCK_PROPERTY_ID | IBLOCK_ELEMENT_ID | VALUE 
88 | 4     | 1     | 22 
89 | 7     | 1     | 14 

所以我需要拉動ID = IBLOCK_ELEMENT_IDIBLOCK_PROPERTY_ID 4 = 22 AND IBLOCK_PROPERTY_ID 7 = 14從第一個表中的結果。我的問題是,我不知道如何將第二個表中的兩行都包含在where子句中,因爲我只能將它用於其中的一個。這是一個:

`SELECT a.ID, a.IBLOCK_ID, a.ACTIVE, a.ACTIVE_FROM, a.ACTIVE_TO, a.NAME, 
b.ID, b.IBLOCK_PROPERTY_ID, b.IBLOCK_ELEMENT_ID, b.VALUE 
FROM b_iblock_element a INNER JOIN b_iblock_element_property b 
ON a.ID = b.IBLOCK_ELEMENT_ID WHERE a.IBLOCK_ID = '3' AND a.ACTIVE = 'Y' 
AND b.IBLOCK_PROPERTY_ID = '4' AND b.VALUE ='22'` 

現在我只需要納入:

和b.IBLOCK_PROPERTY_ID = '7' 和b.VALUE = '14'

但顯然不能只是把它放在另一排,它不起作用。

幫助將不勝感激。

+0

如果我理解正確的話,你需要一個或條款, SELECT a.ID,a.IBLOCK_ID,a.ACTIVE,a.ACTIVE_FROM,a.ACTIVE_TO,a.NAME, b.ID,b.IBLOCK_PROPERTY_ID ,b.IBLOCK_ELEMENT_ID,b.VALUE FROM b_iblock_element一個INNER JOIN b_iblock_element_property b ON a.ID = b.IBLOCK_ELEMENT_ID WHERE a.IBLOCK_ID = '3' 和a.ACTIVE = 'Y' AND (b.IBLOCK_PROPERTY_ID =' 4'AND b.VALUE = 22) OR (b.IBLOCK_PROPERTY_ID ='7'AND b.VALUE = '14') 如果您需要從第二張表中選擇所有行,請使用join join.as @Xavjer說。 – learningNew

+0

正如你想從第二個表中選擇所有條目,我會嘗試一個正確的連接,而不是一個內部連接 – Xavjer

回答

0
SELECT * 
FROM b_iblock_element b1 
JOIN b_iblock_element_property b2 ON b1.ID=b2.IBLOCK_ELEMENT_ID 
WHERE (b2.IBLOCK_PROPERTY_ID=4 AND b2.value=22) OR (b2.IBLOCK_PROPERTY_ID=7 AND b2.value=14);