简体   繁体   中英

Three table MySQL LEFT JOIN not returning rows where one table isn't involved

I have a MySQL database that stores people's orders .

orders may contain:

  • tickets (or no tickets),
  • pledge_type_id (or no pledge_type_id)

An order may have just an additional_donation amount an NO tickets (tickets.order_=order_id) and NO pledge (pledge_type_id) . It should still display...

THE PROBLEM:

However, orders with no tickets (that have either a pledge_type_id or pledge_type_id=0 and additional_donation=[some number] ) are not displaying .

How can I modify this query to return those rows?

Thanks!

SELECT 
o.order_id, o.name_f, o.name_l, o.email, 
COUNT( * ) AS num_tix, 
h.name_f, h.name_l, 
o.additional_donation, 
p.title, p.price, 
o.donation_monthly_YN, 
o.process_step, 
o.paid_status_YNVRD

FROM orders o
LEFT JOIN tickets t ON t.order_id = o.order_id
LEFT JOIN HOSTS h ON h.host_id = o.host_id
LEFT JOIN pledge_types p ON p.pledge_type_id = o.pledge_type_id

WHERE o.gala_id =  '1'
GROUP BY o.order_id
ORDER BY o.deleted_YN ASC , datetime_created DESC 

Here's a fiddle: sqlfiddle.com/#!2/1ea71/1

(It won't allow meto upload data (too big) so here's the sql for the db+data: http://cypgala.com/developer_notes/cypgala_experimental_db.sql (I replaced real names/emails w/fakes))

You probably don't have in one of those tables (hosts or pledge_types) a corresponding tuple for that order. Try RIGHT JOIN on those tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM