ChenXiu Posted March 3, 2022 Share Posted March 3, 2022 Greetings! This one makes my head spin 😀 Scenario: 2 existing mySQL tables of merchandise lists with Primary Key (SKU_number)Note: Both tables often have identical SKU numbers. Desired: New table combining the 2 tables, Primary Key (SKU_number) If the 2 existing mySQL tables contain the same SKU_number, the new Table should have highest price. Example: existing tableONE; +-------------+-------------+-------+ | SKU_number | description | price | +-------------+-------------+-------+ | 222222 | big hammer | 13.00 | | 444444 | good knife | 10.00 | | 666666 | small screw | 8.00 | | 888888 | bolt | 2.00 | +-------------+-------------+-------+ existing tableTWO; +-------------+--------------+-------+ | SKU_number | description | price | +-------------+--------------+-------+ | 111111 | saw | 7.00 | | 222222 | large hammer | 20.00 | | 444444 | good knife | 10.00 | | 666666 | tiny screw | 5.00 | +-------------+--------------+-------+ Brand new master table of both existing tables, and will have: • MAX price, when same SKU_number is in both tables • Product Description from tableONE, when same SKU_number is in both tables +-------------+--------------+-------+ | SKU_number | description | price | +-------------+--------------+-------+ | 111111 | saw | 7.00 | | 222222 | big hammer | 20.00 | | 444444 | good knife | 10.00 | | 666666 | small screw | 8.00 | | 888888 | bolt | 2.00 | +-------------+--------------+-------+ Here is the code I thought had been working, but just now discovered it didn't work when I swapped the table order 😀 create table MaxMaster as SELECT * , MAX(price) FROM ( SELECT * FROM tableTWO UNION ALL SELECT * FROM tableONE ) both_tables GROUP BY SKU_number; Thank you!   Quote Link to comment https://forums.phpfreaks.com/topic/314568-new-table-with-highest-price-from-2-tables/ Share on other sites More sharing options...
ChenXiu Posted March 3, 2022 Author Share Posted March 3, 2022 Oooops. I totally goofed up. Please ignore the above question. Here is what I did: I ran the "create table..." command, but kept getting frustrated that the "price" column didn't change. "select price from....."   why the heck won't it change!?!?!? Duh!OF COURSE the price column doesn't change -- it's the "MAX(price)" column that gets the updated price. This is such a stupid mistake I made. 😀  Quote Link to comment https://forums.phpfreaks.com/topic/314568-new-table-with-highest-price-from-2-tables/#findComment-1594205 Share on other sites More sharing options...
benanamen Posted March 3, 2022 Share Posted March 3, 2022 Why do you have two tables for the same data? Duplicated data is a red flag to a bad DB schema. Learn about Database Normalization. Quote Link to comment https://forums.phpfreaks.com/topic/314568-new-table-with-highest-price-from-2-tables/#findComment-1594206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.