Last active
December 20, 2015 19:18
-
-
Save fillano/6181782 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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