Jump to content

Mysql find best query


asmith

Recommended Posts

Hello :)

 

I have different kind of transactions on the site and their log is recorded in 3 tables: (with different column names)

 

log_a

log_b

log_c

 

Lets say each transaction is for paying a product (id_product). So no matter what kind the transaction is, it is finally gonna point to id_product.

I have another table:

 

paid_products

 

which is pointing that each product is done by which type of transaction:

 

id    transaction_type  transaction_id

3      log_a                    23

41    log_c                    912

 

I want to select all recent id_product which is connected to one of these log tables but have other columns data too.

 

I'm not sure if there is something like:

IF transaction_type = 'log_a'  then inner join log_a on ... OR

IF transaction_type = 'log_b'  then inner join log_b on ... OR

 

So I thought about converting paid_products table to something similar to this:

id    id_log_a    id_log_b    id_log_c

 

 

then run such query:

select *, COALESCE(different field names on log tables) as option1, COALESCE(different field names on log tables) as option2

from paid_products

left join log_a as a on  id_log_a = a.ID_LOG

left join log_b as b on  id_log_b = b.ID_LOG

left join log_c as c on  id_log_c = c.ID_LOG

 

Is there an obvious solution which I'm missing?

Any better idea?

Thanks for your time :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.