Advertisement
akozhomka

Untitled

Oct 22nd, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.96 KB | None | 0 0
  1. SELECT
  2.   cp.ref
  3.   , cp.id
  4.   , cp.description
  5.   , cp.phones AS phone
  6.   , cp.spd_id
  7.   , IFNULL(t.`sum`, 0) AS total_cost
  8. FROM yii_doc.order_ttn_contact_persons AS cp
  9.        LEFT JOIN
  10.      (
  11.        SELECT
  12.             ot.contact_person_id
  13.             , IFNULL(SUM(ot.cost), 0) AS `sum`
  14.        FROM yii_doc.order_contact_person AS ot
  15.             LEFT JOIN yii_doc.order_ttn_contact_persons AS cp ON cp.id = ot.contact_person_id
  16.        WHERE
  17.          DATE(ot.insert_date) = DATE(NOW())
  18.        GROUP BY ot.contact_person_id
  19.      ) AS t ON t.contact_person_id = cp.id
  20.     LEFT JOIN yii_doc.spd_order_ttn_contact_person sotcp ON sotcp.order_ttn_contact_person_id = cp.id
  21.     LEFT JOIN yii_doc.spd ON sotcp.spd_id = spd.id
  22.     WHERE (
  23.             (cp.active = 1
  24.                 AND (spd.invoice = 0 OR spd.invoice IS NULL)
  25.             )
  26.             OR :SPD_ID IS NOT NULL
  27.         )
  28.         AND (:SPD_ID IS NULL OR sotcp.spd_id = :SPD_ID)
  29. ORDER BY total_cost
  30. LIMIT 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement