简体   繁体   中英

How to get active customers ? SQL Query

I'm trying to make some sort of export function for someone, to export his clients to a CSV. Its kinda working, except im not getting the wished results. A debtor can have multiple subscriptions (abonnement). If he has at least one subscription with an end date in the future, it's an active customer, if it has not its an inactive customer.

I now have the following query :

SELECT debiteur.bedrijf_naam, adres.straat, adres.huisnr, 
        adres.huisnr_toev, adres.postcode, adres.plaats FROM debiteur 
LEFT JOIN adres ON adres.debiteur_id = debiteur.id 
LEFT JOIN abonnement ON abonnement.debiteur_id = debiteur.id
LEFT JOIN abonnement_site_rubriek ON abonnement_site_rubriek.abonnement_id = abonnement.id
WHERE adres.adres_rol = 'post' AND
      abonnement_site_rubriek.rubriek_id IN (872,899) AND 
      abonnement.datum_dienst_tot < DATE(NOW())
GROUP BY debiteur.id

With this query, if a customer has any subscriptions with an enddate in the past, even if it has a subscription with a future enddate, it will show up in the inactive customers.

the colum datum_dienst_tot is the subscription end date.

How can i make the query return only inactive customers by looking at all the customers subscriptions?

Who could point me into the right direction ??

Thanks alot!

The given query will bring those who their subscription have already ended, you'd rather check if abonnement.datum_dienst_tot is greater than today as it follows:

SELECT debiteur.bedrijf_naam, adres.straat, adres.huisnr, 
    adres.huisnr_toev, adres.postcode, adres.plaats FROM debiteur 
LEFT JOIN adres ON adres.debiteur_id = debiteur.id 
LEFT JOIN abonnement ON abonnement.debiteur_id = debiteur.id
LEFT JOIN abonnement_site_rubriek ON abonnement_site_rubriek.abonnement_id = abonnement.id
WHERE adres.adres_rol = 'post' AND
    abonnement_site_rubriek.rubriek_id IN (872,899) AND 
    abonnement.datum_dienst_tot > DATE(NOW())
GROUP BY debiteur.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM