In PHP i run this query and get array
with 2 records having 2 keys each
now from this array
i need to extract unique records
is there some more professional way to build this query and then extract unique records from array than what i did below?
P.S. position1
and positions2
tables are identical. so position1
one of them can be removed.
$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");
Dump of $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"
}
}
code continues
$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);
dump of final result
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"
}
}
EDIT:
I need get list of unique positions (pos_id, pos_name) of all positions that assigned to employees position1 and position2 with (for employees with status 1 and 2).
For example employee
John has position1=Driver
, position2=Driver
,
employee Sam has Poition1=Driver
, Position2=Guard
employee Mike has Position1=Cook
, Position2=Driver
so in this case i need to select
List:
4, Driver
19, Guard
2, Cook