Solved

Mysql find and incrementally number matching rows?

Posted on 2010-11-14
7
266 Views
Last Modified: 2012-06-27
I have a table, 'answers' and a field on that table 'q_id', and another field 'num' (nothing is in 'num' right now).

Is there a way to run a query that will find all rows with matching 'q_id' and number them by putting 1 or 2 or 3 or 4 or 5, ... in the 'num' field?  That way I can identify them even though the q_id is the same.

If possible, can you put it together for me please?

Thank you,  Chris

0
Comment
Question by:St_Aug_Beach_Bum
7 Comments
 
LVL 3

Accepted Solution

by:
xeonol earned 500 total points
ID: 34134856
try
set @tmpid =-1; set @tmp = 1; update answers set num = (select @tmp := if(@tmpid<>q_id, 1, @tmp + 1)), q_id=(select @tmpid := q_id) order by q_id;
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34135915
Good news - "num" does not appear to be a reserved word.  Still it's not very descriptive so you might want to choose a word with more meaning.
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

This is pidgin code, but hopefully the design pattern will be clear.  It depends on something you did not mention - namely a UNIQUE field (perhaps an autoincrement key).  If you don't already have one of these, use ALTER TABLE to add one.
// PIDGIN CODE
error_reporting(E_ALL);


$old = mysql_query("SELECT q_id, unique_id FROM answers ORDER BY q_id");
$old_qid = '?';

while ($row = mysql_fetch_assoc($old))
{
    if ($row["q_id"] != $old_qid)
    {
        $old_qid = $row["q_id"];
        $num = 1;
    }
    $uql = mysql_query("UPDATE answers SET num = $num WHERE unique_id = '{$row["unique_id"]}' LIMIT 1");
    $num++;
}

Open in new window

0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34136354
oh, hmmm, I do have a unique field, time_stamp, from the time the questions came in. I can even order by that.

Unfortunitly - I've got to be somewhere in about 20 minutes, so I can't try these solutions out right now. I will later this afternoon though :)

Chris
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34136868
OK - just make sure that the time_stamp column is immutable - once set, it stays set forever and that it is unique - no collisions whatsoever.  Then if those two conditions are true, you can use it for the unique id
0
 
LVL 4

Expert Comment

by:Fugas
ID: 34137719
You should better add an autoincrement field and base your updates on this fields. It will be more efficient when you have more records in your table.

In oracle there you can select the unique rowid, but this option is not implemented in mysql.
0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 34139101
Thank you xeonol, Ray and Fugas.

Xeonol's solution... I was a little hesitant on that one, looked - to - easy... but I ran it first since he posted the first solution and it ran right, with each matching row being numbered correctly, and on the first try!

Thank you!

0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34139115
And thank you Fugas and Ray for the extra tips on the use of 'num' and incrementing.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question