Jump to content

Recommended Posts

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!
 

 

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. 😀
 

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.