Jump to content

combining results of two tables together


blakekr

Recommended Posts

I've never seen this done in code, and I can tell I'm making it way too complicated.

What I want to do (might be?) is simple ...

combine table A with table B, namely:

UPDATE hits in items in table B that have hits in table A -- where items already exist in Table B, with a certain amount of recorded previous hits.

That is, add new hits from Table A to existing ones in Table B.

And then ...

INSERT new items/hits in table B that exist in table A but not B

In other words, feed new items that appear in Table A to Table B, with number of hits intact.

I hope I didn't confuse the issue more than necessary.

Any tips / web sites to look at?

THANKS!
Link to comment
Share on other sites

add new hits from Table A to existing ones in Table B
[code]UPDATE B INNER JOIN A ON B.itemid=A.itemid SET B.hits = B.hits + A.hits[/code]

INSERT new items/hits in table B that exist in table A but not B
[code]INSERT INTO B (itemid, hits)
    SELECT A.itemid, A.hits FROM A LEFT JOIN B ON A.itemid=B.itemid WHERE B.itemid IS NULL[/code]

The first solution just uses a join in the update query, which is perfectly legal.

The second solution uses the INSERT ... SELECT syntax. It also uses LEFT JOIN and IS NULL, so that it only selects rows from A that don't have a match in B.
Link to comment
Share on other sites

[!--quoteo(post=356697:date=Mar 20 2006, 01:31 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 20 2006, 01:31 PM) [snapback]356697[/snapback][/div][div class=\'quotemain\'][!--quotec--]
add new hits from Table A to existing ones in Table B
[code]UPDATE B INNER JOIN A ON B.itemid=A.itemid SET B.hits = B.hits + A.hits[/code]

INSERT new items/hits in table B that exist in table A but not B
[code]INSERT INTO B (itemid, hits)
    SELECT A.itemid, A.hits FROM A LEFT JOIN B ON A.itemid=B.itemid WHERE B.itemid IS NULL[/code]

The first solution just uses a join in the update query, which is perfectly legal.

The second solution uses the INSERT ... SELECT syntax. It also uses LEFT JOIN and IS NULL, so that it only selects rows from A that don't have a match in B.
[/quote]


WHEW ... thank you SO much for your help. This is about as different as can be from the ugly pseudo-code I was trying to cobble together.

Awesome ... THANKS again.
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.