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!