Question

I have a very simple MySQL table with three attributs, let's call it word_tbl

id | word | count

I want to combine each row with all rows in the table and sum the value in count. The results should be saved in a new table, called cartesian_tbl. In my opinion, the cartesian_tbl should look like this:

id | word1_id | word2_id | count

My Problem of understanding are the two word_ids which are both a Foreign Key of word_id.id.

Is my schema correct or is there a simpler solution to reach my target? Thank you for your help!


New:

These 2 tables are only an example...

Here a little example how the combination should work If "Hello" with id 2 has count 4 and "Example" with id 32 has count 5. The new Datarow should look like this 1, 2,32,9

@Francois T count is only a number

What additional informations do you need?

Was it helpful?

Solution

INSERT INTO cartesian_tbl (word1_id, word2_id, count)
SELECT w1.id, w2.id, w1.count+w2.count
FROM word_tbl w1
JOIN word_tbl w2

OTHER TIPS

Create a table with id auto_increment

And something like that :

but what do you want into count ?

insert into cartesian_tbl (world1_id,world2_id,count) select word1.id, word2.id, count??? from word_tbl word1 , word_tbl word

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top