Advertisement
AnthonyWhitebeard

Untitled

Oct 29th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 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 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 order_contact_person AS ot
  15. LEFT JOIN order_ttn_contact_persons AS cp ON cp.id = ot.contact_person_id
  16. WHERE
  17. ot.insert_date = CURRENT_DATE
  18. GROUP BY ot.contact_person_id
  19. ) AS t ON t.contact_person_id = cp.id
  20. LEFT JOIN spd_order_ttn_contact_person sotcp ON sotcp.order_ttn_contact_person_id = cp.id
  21. LEFT JOIN 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