Jump to content

merge data help


gvp16

Recommended Posts

Hi, Ive got 3 tables in a database

 

barcode,  products,  prices  the 3 tables link the data via a product code, so each table has a "productCode" feild.

 

Im looking to put them into a single table containing the contents and fieids from all the tables.

 

BUT!  there is data in some tables that isnt in other ones that I would like to save, so for example, there may be a barcode and product, but no price. or a product and no barcode or price.

 

Anyone know how i can do this?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/219443-merge-data-help/
Share on other sites

You can join the barcodes and prices tables using left joins:

 

select pro.productCode
     , pro.anotherColumn
     -- any other columns from products
     , pri.priceColumn
     -- any other columns from prices
     , bar.barcodeColumn
     -- any other columns from barcodes
from products pro
left join prices pri on (pro.productCode = pri.productCode)
left join barcodes bar on (pro.productCode = bar.productCode);

 

Obviously you'll need to modify it a little, but once done run and make sure it returns the data you want. I weren't sure if by merge you meant permanently within a new table, or for within a select statement; so if you want to physically join them then you can modify the query to create a table using the select statement:

 

create table new_products
    select pro.productCode
         , pro.anotherColumn
         -- any other columns from products
         , pri.priceColumn
         -- any other columns from prices
         , bar.barcodeColumn
         -- any other columns from barcodes
    from products pro
    left join prices pri on (pro.productCode = pri.productCode)
    left join barcodes bar on (pro.productCode = bar.productCode);

Link to comment
https://forums.phpfreaks.com/topic/219443-merge-data-help/#findComment-1137888
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.