• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 796
  • Last Modified:

Slipt table mySQL

Hi,

I have a 'Main Table' with 8 values (per example) and want to split it in two like the image in attachment.

Basically, want I want is to put 1st main table value in Table A, 2nd main table value in Table B, 3th main table value in Table A, etc, etc.... until the last value.
Can you help me?

Thanks,
split-table.png
0
vmorais_
Asked:
vmorais_
1 Solution
 
Serial326Commented:
An easy way to do it would be to make two request (1 for each table)  to create your needed table

See the code below
Table A :
create table Table_A as
SELECT * FROM
(SELECT @row := @row + 1 as row, t.*
FROM Main_Table t, (SELECT @row := 0) r) e
WHERE row mod 2 = 1
 
Table B :
create table Table_B as
SELECT * FROM
(SELECT @row := @row + 1 as row, t.*
FROM Main_Table t, (SELECT @row := 0) r) e
WHERE row mod 2 = 0

Open in new window

0
 
racekCommented:
Serial326 - your solution is brilliant ... maybe step 2 can be done in another way...
Table A :
create table Table_A as
SELECT * FROM
(SELECT @row := @row + 1 as row, t.*
FROM Main_Table t, (SELECT @row := 0) r) e
WHERE row mod 2 = 1
 
create table Table_B as
SELECT * FROM  Main_Table WHERE id not in (SELECT id FROM Table_A);

Open in new window

0
 
vmorais_Author Commented:
excellent, thank you..

I am not just getting to put it in a stored procedure.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now