我有一个名为:workers的表和一个名为:schedule的表,格式如下:
工人:
class Foo {
public:
std::vector<std::function<void()>> bar;
void baz() {};
};
时间表:
| id | name | vacationA | vacationB | workhistory |
| 1 | Florin | 2017-05-05 | 2017-05-25 | 2010-01-01 |
| 2 | Andrei | 2017-06-05 | 2017-06-25 | 2010-01-01 |
| 3 | Alexandra | 2017-07-05 | 2017-07-25 | 2010-01-01 |
| 4 | Emilia | 2017-08-05 | 2017-08-25 | 2010-01-01 |
| 5 | Nicoleta | 2017-09-05 | 2017-09-25 | 2010-01-01 |
+----+-----------+------------+------------+-------------+
我需要查询表“工人”给我2个随机名称,最小班次数,工人不应该在休假期间。工作历史也必须大于18个月。 在这种情况下,我需要的查询应该返回Florin和Andrei。
这是我到目前为止所做的,但它并没有像预期的那样工作:
| day | month | name | shifts |
+-----+-------+-----------+--------+
| 1 | 6 | Florin | 0 |
| 1 | 6 | Andrei | 1 |
| 1 | 6 | Alexandra | 2 |
| 1 | 6 | Emilia | 3 |
| 1 | 6 | Nicoleta | 4 |
+-----+-------+-----------+--------+
此查询返回
SELECT name FROM workers WHERE (CURDATE() NOT BETWEEN vacationA AND vacationB) AND workhistory > (DATE_SUB(CURDATE(), INTERVAL 18 MONTH)) AND name IN (SELECT name FROM schedule ORDER BY shifts LIMIT 2) ORDER BY RAND() LIMIT 2;
。
谢谢!
答案 0 :(得分:1)
由于name
表中已有schedule
列(虽然它不是一个好设计),但您不需要join
。您可以ORDER BY
使用LIMIT
,例如
SELECT name
FROM schedule
WHERE day ? AND month = ? --Remove this if there is no crriteria
ORDER BY shifts
LIMIT 2;
答案 1 :(得分:0)
显而易见的答案是按照班次数对表格进行排序并抓住前两个条目:
SELECT name FROM schedule ORDER BY shifts ASC LIMIT 2
但是,我注意到你已经有一个ORDER BY
子句,所以看起来你想要随机顺序的结果。
如果你也需要随机顺序,那么将整个事物包装在这样的子查询中:
SELECT name FROM (SELECT name FROM schedule ORDER BY shifts ASC LIMIT 2) ORDER BY RAND()