如何组合多个select语句以使用php在表行中回显结果?

时间:2017-03-15 20:28:22

标签: php postgresql

我正在尝试组合来自同一个表的三个select语句,以便使用结果来完成a。我目前正在使用的代码返回:

资源ID#3资源ID#4资源ID#5

我已经对UNION或JOIN做了一些研究,但我还没有发现任何有用的东西。这是我目前的做法:

$statecount = pg_query($conn, "
SELECT 
  COUNT (*) as totalst
FROM 
  fpscdb001_ws_001.incident
WHERE
  incident.initial_symptom = 'Chrome Install' AND
  incident.state_1 = 'MA';");

$sched = pg_query($conn, "
SELECT
  COUNT (*) as totalsch
FROM
  fpscdb001_ws_001.incident
WHERE
  incident.initial_symptom = 'Chrome Install' AND
  incident.state_1 = 'MA' AND
  incident.status_1 = 'Scheduled';");

$closed = pg_query($conn, "
 SELECT
  COUNT (*) as totaldone
FROM
  fpscdb001_ws_001.incident
WHERE
  incident.initial_symptom = 'Chrome Install' AND
  incident.state_1 = 'MA' AND
  incident.status_1 = 'Closed';");

  if (!$statecount) {
          echo "Query failed on state count.\n";
          exit;
        }
  if (!$sched) {
          echo "Query failed on sched.\n";
          exit;
        }
  if (!$closed) {
          echo "Query failed on closed.\n";
          exit;
        }
        {
          echo "<td> $statecount </td>";
          echo "<td> $sched </td>";
          echo "<td> $closed </td>";
        }
pg_close($conn);

1 个答案:

答案 0 :(得分:0)

通过一些试验和错误,我能够使它发挥作用......

$statecount = pg_query($conn, "
SELECT 
  COUNT (*) as totalst
FROM 
  fpscdb001_ws_001.incident
WHERE
  incident.initial_symptom = 'Chrome Upgrade' AND
  incident.state_1 = 'AR' AND
  incident.status_1 != 'Closed'

UNION ALL

SELECT
  COUNT (*) as totalsch
FROM
  fpscdb001_ws_001.incident
WHERE
  incident.initial_symptom = 'Chrome Upgrade' AND
  incident.state_1 = 'AR' AND
  incident.staging = 'True' AND
  incident.is_installed != 'True' AND
  incident.status_1 != 'Closed'

UNION ALL

SELECT
  COUNT (*) as totaldone
FROM
  fpscdb001_ws_001.incident
WHERE
  incident.initial_symptom = 'Chrome Upgrade' AND
  incident.state_1 = 'AR' AND
  incident.staging = 'True' AND
  incident.is_installed = 'True';");

  if (!$statecount) {
          echo "Query failed on state count.\n";
          exit;
        }

        while ($counts = pg_fetch_row($statecount)){
          echo "<td>$counts[0]</td>";
        }