1

在 PHP 中,我运行此查询并获得array2 个记录,每个记录有 2 个键,array我需要从中提取唯一记录是否有一些更专业的方法来构建此查询,然后从数组中提取唯一记录,而不是我在下面所做的?

PS position1positions2表格是相同的。因此position1可以删除其中之一。

$get_positions = "SELECT positions1.pos_id1,
                         positions1.pos_name1,
                         positions2.pos_id2,
                         positions2.pos_name2
                      FROM employees
                          LEFT JOIN positions1 ON positions1.pos_id1 = employees.position1
                          LEFT JOIN positions2 ON positions2.pos_id2 = employees.position2
                              WHERE employees.status IN (1,2)
                                  GROUP BY pos_name2
                                      ORDER BY pos_id2 ASC";
$positions_res = $sql->RunSQL($get_positions, "select");

$positions_res 转储

array(2) {
  [0]=>
  array(8) {
    [0]=>
    string(1) "4"
    ["pos_id1"]=>
    string(1) "4"
    [1]=>
    string(27) "Driver"
    ["pos_name1"]=>
    string(27) "Driver"
    [2]=>
    string(1) "2"
    ["pos_id2"]=>
    string(1) "2"
    [3]=>
    string(9) "Cook"
    ["pos_name2"]=>
    string(9) "Cook"
  }
  [1]=>
  array(8) {
    [0]=>
    string(2) "19"
    ["pos_id1"]=>
    string(2) "19"
    [1]=>
    string(23) "Guard"
    ["pos_name1"]=>
    string(23) "Guard"
    [2]=>
    string(2) "19"
    ["pos_id2"]=>
    string(2) "19"
    [3]=>
    string(23) "Guard"
    ["pos_name2"]=>
    string(23) "Guard"
  }
}

代码继续

$pos_list = array();
$i = 0;
for ($n = 0; $n < count($positions_res); $n++) {

    if ($positions_res[$n]["pos_name1"] == $positions_res[$n]["pos_name2"]) {
        $pos_list[$i]["id"] = $positions_res[$n]["pos_id1"];
        $pos_list[$i]["name"] = $positions_res[$n]["pos_name1"];
        $i++;
    } else {
        $pos_list[$i]["id"] = $positions_res[$n]["pos_id1"];
        $pos_list[$i]["name"] = $positions_res[$n]["pos_name1"];
        $i++;
        $pos_list[$i]["id"] = $positions_res[$n]["pos_id2"];
        $pos_list[$i]["name"] = $positions_res[$n]["pos_name2"];
        $i++;
    }
}

var_dump($pos_list);

最终结果转储

array(3) {
  [0]=>
  array(2) {
    ["id"]=>
    string(1) "4"
    ["name"]=>
    string(27) "Driver"
  }
  [1]=>
  array(2) {
    ["id"]=>
    string(1) "2"
    ["name"]=>
    string(9) "Cook"
  }
  [2]=>
  array(2) {
    ["id"]=>
    string(2) "19"
    ["name"]=>
    string(23) "Guard"
  }
}

编辑:

我需要获取分配给员工 position1 和 position2 的所有职位的唯一职位列表(pos_id、pos_name)(对于状态为 1 和 2 的员工)。

例如员工

约翰有position1=Driver, position2=Driver, 员工 Sam 有Poition1=Driver,Position2=Guard 员工 Mike 有Position1=Cook,Position2=Driver 所以在这种情况下我需要选择

列表:

4,  Driver
19, Guard
2,  Cook
4

2 回答 2

0

Look into the source for $sql->RunSQL(), and I'll bet it calls mysql_fetch_array() without the second parameter. The default behavior of that function is to return both associative and numeric keys unless the constant MYSQL_NUM or MYSQL_ASSOC is passed as the second parameter.

Replace that with a call to mysql_fetch_assoc() or mysql_fetch_array($the_result_resource_variable, MYSQL_ASSOC) to return only associative keys back instead of both associative and numeric keys.

于 2012-07-28T01:37:59.503 回答
0

假设 position1 和 position2 是一个位置表的副本,我会将查询更改为如下所示:

SELECT 
    p.pos_id,
    p.pos_name,
    CAST(GROUP_CONCAT(e.name,'(',e.id,')' SEPARATOR ', ') AS CHAR) AS employees
FROM
    positions p
    JOIN employees e ON e.position1 = p.pos_id OR e.position2 = p.pos_id
WHERE e.status IN (1,2)
GROUP BY p.pos_id
ORDER BY p.pos_id

那么如果员工表看起来像这样:

mysql> select * from employees;
+----+---------+--------+-----------+-----------+
| id | name    | status | position1 | position2 |
+----+---------+--------+-----------+-----------+
|  1 | Glen    |      1 |         1 |         5 |
|  2 | John    |      2 |         7 |         6 |
|  3 | Michael |      1 |         2 |         1 |
|  4 | David   |      0 |         3 |         4 |
|  5 | Albert  |      2 |         2 |         0 |
|  6 | Edward  |      1 |         0 |         3 |
|  7 | Ben     |      1 |         1 |         3 |
+----+---------+--------+-----------+-----------+

职位表如下所示:

mysql> select * from positions;
+--------+--------------+
| pos_id | pos_name     |
+--------+--------------+
|      1 | Janitor      |
|      2 | Guard        |
|      3 | Shift Leader |
|      4 | Driver       |
|      5 | Cook         |
|      6 | Manager      |
|      7 | Owner        |
+--------+--------------+

此查询的结果如下所示:

+--------+--------------+-----------------------------+
| pos_id | pos_name     | employees                   |
+--------+--------------+-----------------------------+
|      1 | Janitor      | Michael(3), Ben(7), Glen(1) |
|      2 | Guard        | Michael(3), Albert(5)       |
|      3 | Shift Leader | Ben(7), Edward(6)           |
|      5 | Cook         | Glen(1)                     |
|      6 | Manager      | John(2)                     |
|      7 | Owner        | John(2)                     |
+--------+--------------+-----------------------------+
于 2012-07-30T22:17:55.623 回答