sql - Mysql query involving multiple tables and an OR statement -


sorry in advance ambiguous title, can't think of one.

i have 2 tables:

bookings(booking_id, customer_id) charges(customer_id, booking_id, amount) 

where in charges table, either booking_id or customer_id must entered. not both.

i'm trying amount associated customer_id

because customer_id's null in charges table, have use booking_id acquire customer_id through bookings table.

so, had query this:

select c.amount charges c, bookings b (c.customer_id = 1234) or  (c.customer_id = null , c.booking_id = b.booking_id , b.customer_id = 1234) 

however, seems create infinite loop.

any suggestions?

thanks,

the problem doing cross join (cartesian product), based on structure of where clause.

a better approach use left outer join (and proper join syntax). so, join bookings table, if exists. use or in where clause match on customer_id in either place:

select c.amount charges c left outer join      bookings b      on c.booking_id = b.booking_id  (c.customer_id = 1234) or (b.customer_id = 1234) 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -