我有下表Orders
,其中包含列:
order_id
,timestamp
,worker_id
,costumer_id
,start_ts
,end_ts
根据start_ts
,end_ts
,worker_id
和costumer_id
的任意组合查询数据库的最佳方法是什么?
如果省略start_ts
,我会:
SELECT *
FROM Orders
WHERE timestamp <= end_ts
AND worker_id = <worker_id>
AND costumer_id = <costumer_id>;
如果省略worker_id
和costumer_id
:
SELECT *
FROM Orders
WHERE timestamp BETWEEN start_ts AND end_ts;
答案 0 :(得分:0)
您可以组合多个条件,如下所示:
SELECT *
FROM Orders
WHERE (timestamp >= <start_ts> or <start_ts> is null)
AND (timestamp <= <end_ts> or <end_ts> is null)
AND ... ;
...
表示尽可能多WHERE
条件,使用AND
组合,如您所愿。