Problem Solution Discussion Selecting a Result Set into an Existing Table

my stmt = SELECT name, birth FROM profile ORDER BY birth DESC LIMIT 4; fetch values into a data structure my ref = dbh-selectall_arrayref stmt; reverse the order of the items in the structure my val = reverse {ref}; use val[i] to get a reference to row i, then use val[i]-[0] and val[i]-[1] to access column values Alt ernat ively, you can sim ply it erat e t hrough t he st ruct ure in reverse order: my stmt = SELECT name, birth FROM profile ORDER BY birth DESC LIMIT 4; fetch values into a data structure my ref = dbh-selectall_arrayref stmt; iterate through the structure in reverse order my row_count = {ref}; for my i = row_count - 1; i = 0; i-- { use ref-[i]-[0] and ref-[i]-[1] here... }

3.22 Selecting a Result Set into an Existing Table

3.22.1 Problem

You w ant t o run a SELECT query but save t he result s int o anot her t able rat her t han displaying t hem .

3.22.2 Solution

I f t he ot her t able exist s, use INSERT INTO ... SELECT , described here. I f t he t able doesnt exist , skip ahead t o Recipe 3.23 .

3.22.3 Discussion

The MySQL server norm ally ret urns t he result of a SELECT st at em ent t o t he client t hat issued t he st at em ent . For exam ple, when you run a query from wit hin m ysql, t he server ret urns t he result t o m ysql, which in t urn displays it t o you on t he screen. I t s also possible t o send t he result s of a SELECT st at em ent direct ly int o anot her t able. Copying records from one t able t o anot her is useful in a num ber of ways: • I f youre developing an algorit hm t hat m odifies a t able, it s safer t o work wit h a copy of a t able so t hat you need not worry about t he consequences of m ist akes. Also, if t he original t able is large, creat ing a part ial copy can speed t he developm ent process because queries run against it will t ake less t im e. • For dat a- loading operat ions t hat work wit h inform at ion t hat m ight be m alform ed, you can load new records int o a t em porary t able, perform som e prelim inary checks, and correct t he records as necessary. When youre sat isfied t he new records are okay, copy t hem from t he t em porary t able int o your m ain t able. • Som e applicat ions m aint ain a large reposit ory t able and a sm aller working t able int o which records are insert ed on a regular basis, copying t he working t able records t o t he reposit ory periodically and clearing t he working t able. • I f youre perform ing a num ber of sim ilar sum m ary operat ions on a large t able, it m ay be m ore efficient t o select sum m ary inform at ion once int o a second t able and use t hat for furt her analysis, rat her t han running expensive sum m ary operat ions repeat edly on t he original t able. This sect ion shows how t o use INSERT ... SELECT t o ret rieve a result set for insert ion int o an exist ing t able. The next sect ion discusses CREATE TABLE ... SELECT , a st at em ent available as of MySQL 3.23 t hat allows you t o creat e a t able on t he fly direct ly from a query result . The t able nam es src_tbl and dst_tbl in t he exam ples refer t o t he source t able from which rows are select ed and t he dest inat ion t able int o which t hey are st ored. I f t he dest inat ion t able already exist s, use INSERT ... SELECT t o copy t he result set int o it . For exam ple, if dst_tbl cont ains an int eger colum n i and a st ring colum n s , t he following st at em ent copies rows from src_tbl int o dst_tbl , assigning colum n val t o i and colum n name t o s : INSERT INTO dst_tbl i, s SELECT val, name FROM src_tbl; The num ber of colum ns t o be insert ed m ust m at ch t he num ber of select ed colum ns, and t he correspondence bet ween set s of colum ns is est ablished by posit ion rat her t han nam e. I n t he special case t hat you want t o copy all colum ns from one t able t o anot her, you can short en t he st at em ent t o t his form : INSERT INTO dst_tbl SELECT FROM src_tbl; To copy only cert ain rows, add a WHERE clause t hat select s t he rows you want : INSERT INTO dst_tbl SELECT FROM src_tbl WHERE val 100 AND name LIKE A; I t s not necessary t o copy colum n values wit hout m odificat ion from t he source t able int o t he dest inat ion t able. The SELECT st at em ent can produce values from expressions, t oo. For exam ple, t he following query count s t he num ber of t im es each nam e occurs in src_tbl and st ores bot h t he count s and t he nam es in dst_tbl : INSERT INTO dst_tbl i, s SELECT COUNT, name FROM src_tbl GROUP BY name; When you use INSERT ... SELECT , you cannot use t he sam e t able bot h as a source and a dest inat ion.

3.23 Creating a Destination Table on the Fly from a Result Set