Skip to content

Instantly share code, notes, and snippets.

@fillano
Last active December 20, 2015 19:18
Show Gist options
  • Save fillano/6181782 to your computer and use it in GitHub Desktop.
Save fillano/6181782 to your computer and use it in GitHub Desktop.
<?php
$units = array(3, 8, 4, 5);
$cn = 0;
$units = array_map(function($a) use(&$cn) {
$cn++;
return array($a, $cn);
}, $units);
$first = array_shift($units);
$s = array_reduce($units, function($res, $item) {
return $res." UNION SELECT ".$item[0].", ".$item[1]." ";
}, "SELECT ".$first[0]." AS id, ".$first[1]." AS uorder ");
$sql = "
SELECT a.id, u.unitName, m.moduleName
FROM ($s) a
LEFT JOIN modules m ON m.unid=a.id
LEFT JOIN units u ON u.id=m.unid
ORDER BY a.uorder;\n";
echo $sql;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment