2017-02-13 48 views
0

我需要幫助來確定運行哪個查詢來搜索客戶。我正在構建一個具有搜索功能的「借錢」計劃。用戶可以通過customerFirstName,customerSecondName過濾顯示在系統中的客戶,以及他們是否處於活動狀態。MySQL:在數據庫中查找活動客戶的查詢

事情是,客戶表中沒有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

+0

它是安全的假設,如果客戶沒有'Active'賬戶,該個Customer 'Inactive'? –

+0

@VitaliiStrimbanu是:) –

回答

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*