Jump to content

Select From One Table, Update Another


Mcod

Recommended Posts

Hello,

 

I am looking for a solution to this:

 

1.) I have a table called font where each font has a unique id called id

2.) I have a table called font_categories_join where each font id (above) has a categoryid

 

What I am trying to do is getting the categoryid into my font table, basically something like

 

updating fontcat (from font table) and setting the fontcat to the value that is in my categoryId inside the font_categories_join table

 

Detailed example:

 

table font contains this:

 

id fontName fontcat

1 some name 0

 

table font_categories_join contains:

fontId categoryId

1 12

 

so what I am trying to do via phpmyadmin is updating the fontcat in the font table from 0 to the correct category 12

 

Your help would be appreciated :)

Link to comment
Share on other sites

I must confess that I'm curious as to why you'd want to duplicate data like this? I don't see any specific advantage of this, but only a disadvantage as you've just created a possible data inconsistency point.

What happens if the category ID in the font table no longer matches the category ID and font ID combination in the font_categories table?

 

Not to mention you just invalidated the many-to-many relation you apparently were using prior to this.

Link to comment
Share on other sites

@Barand, thank you for your solution - it worked really great :)

 

@Christian F. The reason is saving queries - Categories will not change and this way I can get all details with one simple query without the need to do additional queries on another table. This sometimes matters if you run a large site and you have to watch how many queries you do. I understand what you are saying, but in my case this is the better solution.

 

Thanks again :)

Link to comment
Share on other sites

What happens if the category ID in the font table no longer matches the category ID and font ID combination in the font_categories table?

 

Not to mention you just invalidated the many-to-many relation you apparently were using prior to this.

 

My assumption was that is 1 to many relationship and the join table has been found to be redundant

Link to comment
Share on other sites

Mcod: Are you using JOINs, or running queries inside loops? If the latter, then the solution you've opted for in this thread is not the solution, but a poor workaround. If the former, you might want to be looking at caching your results instead. Considering the amount of traffic you must be getting, and the correct (first step) solution to that particular issue is caching.

 

Now, if Barand's assumption is correct, and that this is really a one-to-many relation then what you've done here is correct. Though, traffic loads really don't have any relevance on the correctness of the fix in this case.

 

Barand: Yep, figured so. Though, I don't like to assume, thus I asked. ;)

Link to comment
Share on other sites

  • 2 weeks later...
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.