Sql optimization: add judgment when querying

1. Structure of the data table:
order ID:orderid,
user ID:memberid,
date of order issued: order_date

2. Demand: need to query the record of orders issued from 2017 to April (orderid,memberid). If the user of the current record has placed an order before April, whether the third column has ordered: yes, if it has not placed an order before April, whether the third column has ordered: no,

3. Sql: that has been written

SELECT orderid,memberid, "" AS "" FROM ordersum WHERE order_date >= "2017-04-01" AND order_date < "2017-05-01" AND memberid IN (SELECT memberid FROM ordersum WHERE order_date < "2017-04-01")
UNION
SELECT orderid,memberid, "" AS "" FROM ordersum WHERE order_date >= "2017-04-01" AND order_date < "2017-05-01" AND memberid  NOT IN (SELECT memberid FROM ordersum WHERE order_date < "2017-04-01")
ORDER BY orderid

can achieve this requirement, just feel that there is still room for optimization, please help to have a look, thank you!

Mar.20,2021

if of mysql.


select orderid, memberid, bool_judge from (select orderid, memberid, case when order_date <= '2017-04-01' then '' else '' end as bool_judge) as test where order_date >= '' and order_date <= ''
Menu