473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

MySQL, pull data from a column in a different table only if a second column matches

Okay, so I've got two tables. One is called "orders_products" and the other is called "products". They contain different fields but have a couple that match up. When an order is placed, orders_products is filled with a bunch of data about the item in question - including a field called 'products_id' which is unique to the different items. The "products" table has less information about orders (no quantity field or tax amounts), but more information about the items.

What I have done is added two new fields into "orders_products" that need to contain a part number and an old part number (called product_part & product_old_part respectively). Going forward, all new orders will fill in these fields, but what I can't figure out is how to fill in the fields for thousands of previous orders.

Somehow I need to tell MySQL to look at the "orders_products" table and find the 'products_id' field. Then find a match for that number in the 'products_id' field of the "products" table. When it finds a match, it needs to take the values listed for 'product_part' and 'product_old_part' from the "products" table and insert it into the corresponding fields in the "orders_products" table.

Any thoughts on how to accomplish this? Seems like it should be easy, but that I'm missing the obvious. Any help would be appreciated. Thanks

Here's what the tables kind of look like:

orders_products
Expand|Select|Wrap|Line Numbers
  1. --------------------------------------------------------
  2. orders_id| products_id| orders_tax| products_part| etc |
  3. --------------------------------------------------------
  4.    1           12         0.34          NULL       ...
  5.    2           13         0.64          NULL       ...
  6.    3           14         3.52          NULL       ...
  7.    4           15         9.76          NULL       ...
  8.   ...          ...        ...           ...        ...
  9. --------------------------------------------------------
  10.  

products
Expand|Select|Wrap|Line Numbers
  1. --------------------------------------------------------
  2. products_id |   products_model |   products_part | etc |
  3. --------------------------------------------------------
  4.    12               IB45              47592N24     ...
  5.    13               SN59              08572E84     ...
  6.    14               OI18              84083R09     ...
  7.    15               PE97              08647W38     ...
  8.   ...               ...               ...          ...
  9. --------------------------------------------------------
  10.  

EDIT: There was a duplicate 12, sorry about that.
Mar 3 '11 #1

✓ answered by dgreenhouse

I noticed that you have a field named products_model in the products table and that there are 2 rows with products_id = 12. This; I assume, could add an additional complexity.

I haven't tested this, but this might be a start.

Also, if your tables are InnoDB you might have to use the "on update" clause.

Finally, I think you might want to consider normalizing your database schema.
If I'm correct, you'll paint yourself into a corner as things progress.

Expand|Select|Wrap|Line Numbers
  1. UPDATE orders_products,products SET orders_products.products_part = products.products_part,
  2. orders_products.old_products_part = products.old_products_part
  3. WHERE orders_products.products_id = products.products_id and {how do you differentiate products_model} and isnull(orders_products.products_part)
  4.  
Just tested and it works; except for the the products_model issue I mentioned.

2 1773
dgreenhouse
250 Expert 100+
I noticed that you have a field named products_model in the products table and that there are 2 rows with products_id = 12. This; I assume, could add an additional complexity.

I haven't tested this, but this might be a start.

Also, if your tables are InnoDB you might have to use the "on update" clause.

Finally, I think you might want to consider normalizing your database schema.
If I'm correct, you'll paint yourself into a corner as things progress.

Expand|Select|Wrap|Line Numbers
  1. UPDATE orders_products,products SET orders_products.products_part = products.products_part,
  2. orders_products.old_products_part = products.old_products_part
  3. WHERE orders_products.products_id = products.products_id and {how do you differentiate products_model} and isnull(orders_products.products_part)
  4.  
Just tested and it works; except for the the products_model issue I mentioned.
Mar 3 '11 #2
Expand|Select|Wrap|Line Numbers
  1. UPDATE orders_products,products SET orders_products.products_part = products.products_part,
  2. orders_products.products_old_part = products.products_old_part
  3. WHERE orders_products.products_id = products.products_id and isnull(orders_products.products_part)
  4.  
This seems to have worked perfectly, thank you very much dgreenhouse!
Mar 3 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: E | last post by:
I have two tables and i want join the two of the primary id's in to one table. the database is mysql. ex. Table Item (ID int(10) NOT NULL auto_increment) Table Actor(ID int(10) NOT NULL...
5
by: cvillard | last post by:
I am new to SQL, as old as it is. I am not new to programming I normally just use Access. I have two tables for a little project manager I made. After updates I sent an email to the user. I need...
4
by: sheree | last post by:
I have 3 tables (amoung a few others) in a small access database. The tables are as follows: == AEReport -------- AEID (PK) RptCatelog GCRCID PatientID EvntDate
1
by: Nick J | last post by:
Hi, I have a command button, when I press it I would like it to find the record I have specified in a text box on the current form and to pull fields such as Address 1 and Address 2 from another...
17
by: Wilfried | last post by:
Hi, I have 5 tables: main data1
3
by: jjgardner3.web | last post by:
Hello I have a very simple xml file: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="./status.xsl"?> <IEC> <Workstation> <Hostname>HOSTNAME</Hostname>...
2
by: Prashantsd | last post by:
Hi, I just wanted to know if it is possible to save the form data into different table. The combox box list are from the different tables and want save in another new table. What are the...
12
by: Randy | last post by:
Hi, Trying to pass along a table row delete to the datasource, but I'm crashing. Here is the code: Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
6
by: BinaryGirl23 | last post by:
Hi Everyone : ) I hope that this question is actually something easy in disguise. Re: Access 2003 I have a report that displays information about which of our employees worked at a given job over...
5
by: lawson | last post by:
In access 2003, how do I repeat the same data in different tables without typing it out again. That in another table with different name.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.