2017-04-02 11 views
1

私は2つのテーブル:customerscustomers_historyを持っています。各テーブルの最新の「シーケンス」の最初のレコードを取得するためのMySQL「バックトラック」

列:

customers.idcustomers.email

customers_history.datecustomers_history.user_idcustomers_history.renewal_error

私はrenewal_error状態に現在ある顧客IDの配列があります$failing_customers_ids = array(10, 20);

を得意先のレコードの例::

Date user_id renewal_error 28-03-17 10 Insufficient Funds 29-03-17 10 Insufficient Funds 30-03-17 10 NULL 31-03-17 10 Insufficient Funds 01-04-17 10 Insufficient Funds 31-03-17 20 Insufficient Funds 01-04-17 20 Insufficient Funds

私は、この場合には、クエリはにつながるので、電子メールなどの顧客の詳細情報を照会する必要があり、現在の失敗シーケンスの最初の更新エラーの日:、

email: [email protected]date: 31-03-17

email: [email protected]date: 31-03-17

私はテーブルを結合しようとしたし、「バックトラック」するためにPHPを介して処理を行いましたし、私が望むものを手に入れることができますが、MySQL経由でこれをすべて達成できるかどうかを楽しみにしています。

答えて

0

クエリは次のようになります。実際には、既知のエラーはありません

$sql = "SELECT DISTINCT * from customers 
join customers_history 
on customers.id = customers_history.user_id 
where customers.id IN (:ids) 
and customers_history.renewal_error = :error 
ORDER BY customers_history.date ASC" 
+0

、私は私の例では唯一つのエラーを使用しますが、エラーメッセージが(何もすることができます)は不明です。 –

+0

このクエリをphpmyadminに直接実行してみてください –

関連する問題