Assuming I have table "Department" (200 row) & "Employees" (300,000 row) each has their own details And the vector table combining both is "dep_emp"
CREATE TABLE IF NOT EXISTS `dep_emp` (
`id` int(11) NOT NULL DEFAULT '0',
`dep_id` int(11) NOT NULL DEFAULT '0',
`emp_id` int(11) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
My question is how to make a query to get two of employees (any two employees) for every Department? My solution so far is this:
(SELECT * FROM `dep_emp` WHERE dep_id=1 LIMIT 2) UNION ALL
(SELECT * FROM `dep_emp` WHERE dep_id=2 LIMIT 2) UNION ALL
(SELECT * FROM `dep_emp` WHERE dep_id=3 LIMIT 2) ........etc
of course this is done {n} times for each Dept so if I wanted to display two Employees for all dept then I will have to write 199 UNION ALL, this is very ugly Any other solutions PS. there are a lot of details that I have moved out of the problem to make things a lot simpler