2017-02-13 9 views
0

顧客を検索するために実行するクエリを把握するのに役立つ必要があります。私は、検索機能を持つ「お金を貸す」プログラムを構築しています。ユーザーは、customerFirstName、customerSecondName、およびそれらがアクティブであるかどうかによって、システムに表示される顧客をフィルタリングできます。MySQL:データベース内のアクティブな顧客を検索するクエリ

事は、statusの列が顧客表にありません。 1つ以上のアカウントがstatus = 'Active'の場合、costumerはActiveとみなされます。 彼らはすべてのアカウントを開設していないか、すべて自分のアカウントが

私の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'アカウント、顧客のを持っていない場合と仮定することは安全です「非アクティブ」? –

+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* 
関連する問題