Если я правильно понимаю, вы хотите показать данные о работнике с самой низкой зарплатой по каждой профессии, правильно? В этом случае мы можем использовать группу от GROUP BY
до по нашей профессии и ORDER BY
для заказа по зарплате.
Пример данных примера:
CREATE TABLE tbl_employees(id int(11), name varchar(100), occupation varchar(100));
CREATE TABLE tbl_salary(id int(11), salary decimal);
INSERT INTO tbl_employees VALUES(1, "John Doe", "Accountant");
INSERT INTO tbl_employees VALUES(2, "Christopher Stark", "Accountant");
INSERT INTO tbl_employees VALUES(3, "Arnold Sanders", "Accountant");
INSERT INTO tbl_employees VALUES(4, "Jane Doe", "Marketer");
INSERT INTO tbl_employees VALUES(5, "Christina Lavine", "Marketer");
INSERT INTO tbl_employees VALUES(6, "Penny Lane", "Marketer");
INSERT INTO tbl_employees VALUES(7, "George Franklin", "Customer Support");
INSERT INTO tbl_employees VALUES(8, "Kent Brockmann", "Customer Support");
INSERT INTO tbl_employees VALUES(9, "Richard Felenski", "Customer Support");
INSERT INTO tbl_employees VALUES(10, "Patrick Neil", "Technical Support");
INSERT INTO tbl_employees VALUES(11, "Paul Richards", "Technical Support");
INSERT INTO tbl_employees VALUES(12, "Tom Harris", "Technical Support");
INSERT INTO tbl_employees VALUES(13, "Rebecca Moore", "Project Manager");
INSERT INTO tbl_employees VALUES(14, "Laura Cross", "Project Manager");
INSERT INTO tbl_employees VALUES(15, "Denise McIntyre", "Project Manager");
INSERT INTO tbl_salary VALUES(1, 100.00);
INSERT INTO tbl_salary VALUES(2, 150.00);
INSERT INTO tbl_salary VALUES(3, 300.00);
INSERT INTO tbl_salary VALUES(4, 110.00);
INSERT INTO tbl_salary VALUES(5, 120.00);
INSERT INTO tbl_salary VALUES(6, 235.00);
INSERT INTO tbl_salary VALUES(7, 240.00);
INSERT INTO tbl_salary VALUES(8, 200.00);
INSERT INTO tbl_salary VALUES(9, 160.00);
INSERT INTO tbl_salary VALUES(10, 230.00);
INSERT INTO tbl_salary VALUES(11, 320.00);
INSERT INTO tbl_salary VALUES(12, 150.00);
INSERT INTO tbl_salary VALUES(13, 168.00);
INSERT INTO tbl_salary VALUES(14, 175.00);
INSERT INTO tbl_salary VALUES(15, 350.00);
Запрос:
SELECT
tbl_employees.id as emp_id,
tbl_employees.name as emp_name,
tbl_employees.occupation as emp_pos,
tbl_salary.salary as emp_salary
FROM
tbl_employees
INNER JOIN
tbl_salary ON tbl_employees.id = tbl_salary.id
GROUP BY
tbl_employees.occupation
ORDER BY
tbl_salary.salary DESC
Это должно дать вам каждого работника с самой низкой зарплатой в каждой уважаемой профессии.
Результат :
Рабочая скрипка здесь .