私は最近、Yii を使用してプロジェクトを開始し、クエリ ビルダーに慣れようとしています。今、結合を使用してクエリを作成し、クエリで結合テーブルのデータにアクセスしたいのですが、次を機能させることができませんでした:
私の(簡略化された)dbテーブル:
customer(#id, name)
employee(#id, name)
customer_employee(#customerid, #employeeid)
accounting(#id, customerid, started_date, finished_date, month, year)
- 顧客と従業員の間の多対多の関係
- 顧客と経理の1対多の関係
次のクエリを実行すると、特定の従業員に関連付けられているすべての顧客が選択され、該当する場合は会計ステータス (started_date と finished_date) が表示されます (そうでない場合は null)。
次のクエリは完全に機能しますが、cdbcriteria と Yii クエリ ビルダーで動作させることができないだけです: (また、ハードコードされた ID はこの例のためだけのものです)
SELECT name, started_date, finished_date
FROM customer
RIGHT JOIN customer_employee ON customer.id=customer_employee.customerid
LEFT JOIN accounting ON customer.id=accounting.customerid
WHERE customer_employee.employeeid=2';
助けてください!