Jump to content

Mysql Query help...


ajlisowski

Recommended Posts

So I need some help trying to develop a query which may not even be possible...

 

Basically I have a number of tables which represent a product. Ill simplify things with dummy tables. I have a product table which contains product information, a product_vendor table which connects a vendor to a product based on region. Then a pricing table which gives prices based on the product_vendor relationship. This allows multiple vendors to set their price per region.

 

 

product

id,

product_sku

product_name,

manufacturer,

 

product_vendor

product_to_vendor_id,

product_sku,

vendor,

region,

 

price

product_to_vendor_id

price

 

The tricky part is, I also have a modifications table. Which allows users to modify product information to be approved by an admin user later.

 

modification

product_sku,

table,

field,

value,

user

 

So it stores what field of the product has been modified and by who.

 

I want to write a query that gets all the product info, but replaced fields with the corresponding modifcation entry if it exists.  Im not sure if a mysql query can get the job done in one go.

 

Perhaps I would need to get all the data, then get all the modifications and run through them replacing the data of the first results with the corresponding value of the modifications table? I would like to do it in one query but im not sure thats possible. Any help would be great..

Link to comment
Share on other sites

Off the top of my head, not sure this will work but maybe this will get you started in the right direction.  (Or maybe it will get you started in the wrong direction...)

 

select if(modification.field = 'product_name' and modification.value is not NULL, modification.value, product.product_name) product_name,
       if(modification.field = 'manufacturer' and modification.value is not NULL, modification.value, product.manufacturer) manufacturer
from product
left join modification on (modification.product_sku = product.product_sku and modification.table = 'product')
group by product.id

 

I'm assuming you're linking the modification to the product and product_vendor tables via the product_sku; the price table would be a bit more complex but not too much, if the above query works it shouldn't be too hard to extend it to work for the price table.

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.