我有两个查询:
1)首先我找到所有独特的name
并计算每个独特name
的Table
2)第二个我得到每个唯一的行name
代码:
$sql1 = "SELECT name, count(name) as count
FROM (
SELECT name
FROM Table
WHERE id_city='333' AND id_firm='444' AND id_service='555'
) x
GROUP BY name
";
$res1 = sqlsrv_query($conn, $sql1);
while ($i = sqlsrv_fetch_array($res1, SQLSRV_FETCH_ASSOC))
{
$name = ($i['name'] != '') ? $i['name'] : '0';
$sql2 = "SELECT
ta.unit as ta_unit,
ta.id_producer_goods as ta_id_producer_goods
FROM Table ta
WHERE ta.id_city='333'
AND ta.id_firm='444'
AND ta.id_service='555'
AND ta.name='$name'";
$params = array();
$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$res12 = sqlsrv_query($conn, $sql12, $params, $options);
}
带有结构表的 Sql Fiddle HERE
可以从两个查询中做一个查询吗?