简体   繁体   中英

MYSQL : How to set to same position if row value is the same?

position    | Average |   gpmp

1             70.60         2.0     
2             60.20         2.3    
3             59.80         4.8    
4             59.80         4.8     
5             45.70         5.6     

Hie All,

As above table, I need to arrange the position according to the lowest gpmp and the highest average. But when the both average and gmp are the same, I will need to have the position to be the same.

For example, position 3 and 4 have the same average and gpmp. How do I generate the mysql query or using php function so that after they detect the same average and gpmp and change the position 4 to 3.

Which mean after the function is generated it will become like the table below.

position    | Average |   gpmp

1             70.60         2.0     
2             60.20         2.3    
3             59.80         4.8    
3             59.80         4.8     
5             45.70         5.6    

Here's a simple way to update the table as you described in your post - taking the sequential positions and updating them accordingly. It doesn't calculate the positions or anything, just uses the data already there:

UPDATE `table` t SET position = (
    SELECT MIN(position) FROM (SELECT * FROM `table`) t2 WHERE t.Average = t2.Average AND t.gpmp = t2.gpmp
)

(pseudo code) select * from table
get output into php var
foreach (php row of data)
is row equal to previous row?
yes - don't increment row counter, increment duplicate counter
no - increment row counter with # of duplicates and reset duplicate counter
save current row as 'previous row'
next

I'd give something like the following a try, through it does assume a primary key is on this table. Without a primary key you're going to have issues updating specific rows easily / you'll have a lot of duplicates.

So for this example I'll assume the table is as follows

someTable (
    pkID (Primary Key),
    position,
    Average,
    gpm
)

So the following INSERT would do the job I expect

INSERT INTO someTable (
    pkID,
    position
)
SELECT
    someTable.pkID,
    calcTable.position
FROM someTable
INNER JOIN (
    SELECT 
        MIN(c.position) AS position,
        c.Average,
        c.gpm
    FROM (
        // Calculate the position for each Average/gpm combination
        SELECT
            @p = @p + 1 AS position,
            someTable.Average,
            someTable.gpm
        FROM (
            SELECT @p:=0
        ) v,someTable
        ORDER BY 
            someTable.Average DESC, 
            someTable.gpmp ASC
    ) c
    // Now regroup to get 1 position for each combination (the lowest position)
    GROUP BY c.Average,c.gpm
) AS calcTable

// And then join this calculated table back onto the original
ON (calcTable.Average,calcTable.gpm) = (someTable.Average,someTable.gpm)

// And rely on the PK IDs clashing to allow update
ON DUPLICATE KEY UPDATE position = VALUES(position)

you can try something like this in php:

$d= mysql_query('select distinct gpmp from tablename order by gpmp');

pos= 1;

while($r= mysql_fetch_array($d)){

   mysql_query('update tablename set position='.$pos.' where gpmp='.$r['gpmp']);

   $pos++;
}

You only need to "expand" the idea to take averange in account too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM