加入2选择语句和不同的Where子句&在While循环中使用

时间:2015-06-29 15:37:54

标签: php mysql

我有2个select语句,但我需要从一个查询中检索两组日期,这样我就可以执行while循环并返回每行中的两个日期。

声明一:

SELECT a.ID, a.CREATED_BY, a.IBLOCK_ID, a.NAME, 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 b.IBLOCK_PROPERTY_ID = '133'

然后第二个是相同的但具有不同的where子句:

SELECT a.ID, a.CREATED_BY, a.IBLOCK_ID, a.NAME, 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 b.IBLOCK_PROPERTY_ID = '134'

b.VALUE字段返回IBLOCK_PROPERTY_ID为133时的开始日期和等于134时的结束日期。

我已经读过我可以使用CASE或UNION但不确定这是如何工作的。我显然将b.VALUE用作STARTDATE,将b.VALUE用作ENDDATE但不确定如何将它们连接在一起。

然后我将使用PHP来循环结果:

$result = mysqli_query($con,"QUERY");
while($row = mysqli_fetch_array($result))
{

}

1 个答案:

答案 0 :(得分:1)

嗯,你既不需要CASE也不需要UNION。您只需要另一个INNER JOIN。这是加入查询:

SELECT a.ID, a.CREATED_BY, a.IBLOCK_ID, a.NAME, b.VALUE as startDate, c.VALUE as endDate
FROM b_iblock_element a INNER JOIN b_iblock_element_property b 
ON a.ID = b.IBLOCK_ELEMENT_ID AND b.IBLOCK_PROPERTY_ID = '133'
INNER JOIN b_iblock_element_property c 
ON a.ID = c.IBLOCK_ELEMENT_ID AND c.IBLOCK_PROPERTY_ID = '134'