Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql - UPDATEing row based on other rows
    primarykey
    data
    text
    <p>I'm looking to update certain rows based on specific criteria based on other rows. Lets say the table looks like this:</p> <pre><code>COLUMNS: time type genre doubles triples ROW 1: 2010.06.21 12:00 1 1 0 0 ROW 2: 2010.06.21 12:00 1 2 0 0 ROW 3: 2010.06.21 12:00 1 1 0 0 ROW 4: 2010.06.21 12:00 2 3 0 0 ROW 5: 2010.06.22 12:00 2 2 0 0 ROW 6: 2010.06.22 12:00 2 3 0 0 ROW 7: 2010.06.22 12:00 1 1 0 0 </code></pre> <p>I'm looking to update the <code>doubles</code> and <code>triples</code> columns based on the following rules: </p> <p>1) Only look to update rows where the <code>time=time</code> and <code>type=type</code> between rows (e.g. Rows 1,2,3 and rows 5 &amp; 6). </p> <p>2) Next count the # of different <code>genre</code>'s between those rows and if there are two different <code>genres</code> then change the <code>doubles</code> column to 1, or if there are three then change the <code>triples</code> column to 1. SO for example in the table above rows 1,2,3 would have <code>doubles=1</code> because between the three rows there are two different genres. Rows 5 and 6 would also have <code>doubles=1</code> because there is again two different genres between the rows. <code>Doubles</code> can =1 and <code>triples</code> can =1 but not both.</p> <p>Now, I could write up some PHP based on these rules pretty easily I think, but I'm wondering if there is a way to do it all in mysql? It seems like I'm always surprised the amount you can do from a SQL statement.</p> <p>Maybe something like (two different statements for the <code>doubles</code> and <code>triples</code> column):</p> <p>Doubles - <code>UPDATE myTable SET (doubles=1) WHERE time=time AND type=type</code> ... but then how would you account for rule #2 above (counting number of rows with unique genres).</p> <p>Is this possible in mysql or is PHP just the right way to go here?</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload