MySQL:查找数据库中活动客户的查询

时间:2017-02-13 15:27:26

标签: mysql sql database select

我需要帮助确定要搜索客户的查询。我正在建立一笔“借钱”#34;具有搜索功能的程序。用户可以通过customerFirstName,customerSecondName以及它们是否处于活动状态来过滤系统中显示的客户。

问题是,客户表中没有status列。如果客户的一个或多个帐户状态为“有效”,则客户被视为有效。 如果客户未打开任何帐户或其所有帐户都具有状态='支付'

,则该客户被视为无效

我的SELECT语句是:

SELECT 
customer.customerID,
customerFName,
customerLName,
gender,
civilStatus,
birthDate,
homeAddress,
jobDescription,
workingAddress,
telNumber,
phoneNumber,
pinNumber

在这种情况下,客户会按名字和姓氏进行过滤:

WHERE customerFName LIKE '%*userProvidedFirstName*%' AND customerLName LIKE '%*userProvidedLastName*%'

这是我的数据库架构:

This is my database ERD

1 个答案:

答案 0 :(得分:1)

您可以尝试在选择部分中获取状态,然后根据子选择进行过滤:

select * from
    (SELECT 
    customer.customerID,
    customerFName,
    customerLName,
    gender,
    civilStatus,
    birthDate,
    homeAddress,
    jobDescription,
    workingAddress,
    telNumber,
    phoneNumber,
    pinNumber
    case when (select count(*) from account where account.customerID = customerID and account.status = 'Active') > 0 
    then 'Active' else 'Inactive' end as status
from customer)
    WHERE customerFName LIKE '%*userProvidedFirstName*%' AND customerLName LIKE '%*userProvidedLastName*%' AND status = *Status*