-1

I've got a big database I am trying to split it, this big database has a column where each row is either bid or buy it now. I want to split up this table into two tables, I was thinking of doing a SQL statement such as INSERT INTO newtable WHERE row[5] = 'bid' for example. But after some research I found out there is not where statement in an insert statement. Is that true? IF not how can I split up this database? (Using python2 btw)

Coder77
  • 2,203
  • 5
  • 20
  • 28

3 Answers3

1

Why don't you SELECT each group first and then loop through the results and INSERT them?

As per our conversation in chat you could do something like this:

all = "SELECT * FROM listings1 WHERE auctionType IN ('Bid', 'BuyNow')" 
cursor.execute(all)
results = cursor.fetchall()
for row in results:
    if row['auctionType'] == 'Bid':
      #do your insert here
      insert = "INSERT INTO listings1 () VALUES (domainName, itemID)" % (row['domainName'], row['itemID'] etc etc)
      cursor.execute(insert)
    elif row['auctionType'] == 'BuyNow':
      #other insert here

Also note that your mysql library matters. You want the results returned as a dict. See this answer for more details: https://stackoverflow.com/a/8623947/1086938

Community
  • 1
  • 1
hanleyhansen
  • 6,304
  • 8
  • 37
  • 73
1

Insert statement has now where clause, but there is INSERT INTO SELECT statement: INSERT INTO newtable (...) SELECT ... FROM oldtable WHERE column = 'bid'

Alex Shkop
  • 1,992
  • 12
  • 12
  • Unsure what I'd put in the '..' bits. – Coder77 May 20 '14 at 16:23
  • List of column names: `INSERT INTO newtable (column1, column2, column3) SELECT column1, column2, column3 FROM oldtable WHERE column = 'bid';`. Note that the number of columns in `INSERT` and `SELECT` should be equal. – Alex Shkop May 21 '14 at 06:07
0

Not sure if maybe I am misunderstanding the question... but you cannot use WHERE with an INSERT. WHERE is just used to help specify which rows you want to grab or alter in a query.

If what you want to do is grab all the entries in the table that have X value for a particular column (like 'bid' for row[5]) to then insert into their own table elsewhere, you can do a loop on a SELECT statement to grab all of those values with a WHERE row[5]='bid'. (SELECT * FROM currenttable WHERE row[5]='bid').

In a while loop, you can then INSERT INTO newtable (column,column,column) VALUES (value,value,value) - replacing the column/value for all the columns/values for that table.

Once finished, and confirmed that the new table has the appropriate values, you can remove/delete all the unnecessary entries from your original table (DELETE FROM currenttable WHERE row[5]='bid') and thus you have split your table.

princessjackie
  • 565
  • 3
  • 13