Question

Okay, so I have two tables. Lets call them test2 and test2history.

test2 looks like this: MySQL table test2
(source: gyazo.com)

test2history looks like this: MySQL table test2history

I need to pull multiple cells from table test2 and make them go into one single cell in test2history. The week column from test2 would be set to the value of week in test2history.

The day column from test2 translates to:

  • 0 = Not Set
  • 1 = Monday
  • 2 = Tuesday
  • 3 = Wednesday
  • 4 = Thursday
  • 5 = Friday

On the site that I am working on, when submit is clicked it should pull all cell data from test2 where week is the $_GET['week'] in the URL. Which is easy, I have that. But then, it needs to search the rows that it found and then put the 'map' values whose 'day' value are the same into the corresponding weekday in test2history. Let me stop there because I know this is confusing the way I am explaining it, let me make you a visual:

This is how I need it to work: enter image description here

The 'total' column in test2history should be set as the sum of the cards from test2 that had that same week number. Which I can do that myself, I just wanted to make sure you understand what is happening.

I have read up on some group_concat() which seems like what I need to be using but I just couldn't find a good explanation that related to what I need it to do. That may not even be what I need.

My query I struggled to write isn't correct in any way shape or form but I tried and I have no idea how go about this one, hopefully one of you guys do. Thanks for your help.

Was it helpful?

Solution

I solved this problem a while back by using GROUP_CONCAT.

    UPDATE test2history SET monday = (SELECT group_concat(map, ' ', cards SEPARATOR ', ') FROM test2 WHERE test2.day=1 AND test2.week=$week)

I simply repeated this query for each day.

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