Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 10-10-2012, 02:16 AM
kronnos kronnos is offline
 
Join Date: Apr 2006
Posts: 241
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default SQl query help

Can someone help with a query to import data into a subscriptions table. It shoudl only update but not overwrite if data for that user already exists.

1) I would either want to migrate date from a certain table>field to a different table>field where the user ID filed matches.

Or the longer way...

2) Export all the data from the 3 different tables that make up all the subscription data and then somehow import that back into the default vBulletin subscription tables.

Any advice or help with query?

Thanks in advance!
Reply With Quote
  #2  
Old 10-11-2012, 08:16 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't quite understand what you need. Are you talking about adding records to your subscribeforum and subscribethread tables? Where would the info be coming from?
Reply With Quote
Благодарность от:
kronnos
  #3  
Old 10-11-2012, 02:07 PM
kronnos kronnos is offline
 
Join Date: Apr 2006
Posts: 241
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ok after about 6 hours, I think I figured out exactly what fields i would need to move the data from and to.

I would like to move data from the "From_subscriptions_data" table to the "To_subscriptionlog" table. I specified the default values for field that would not be imported from the "From_subscriptions_data" table.

The next move is from the one table "From_payments" to two different table called "To_paymenttransaction" and "To_paymentinfo". A lot of the field should just have default data that i specified.

Below is the chart with the table names and the fields.
Looks a lot cleaner here: https://docs.google.com/spreadsheet/...W01bWI3ODFhN2c

To_subscriptionlog ----- < ----- From_subscriptions_data
subscriptionlogid --------- < ----- id
subscriptionid ------------ < ----- subid
userid ------------------- < ----- userid
pusergroupid ------------ < ----- termid
status ------------------ < ----- active
regdate ----------------- < ----- stime
expirydate -------------- < ----- etime
importsubscriptionlogid --- < ----- (Set all to "0")


To_paymenttransaction --- < --- From_payments
paymenttransactionid ------- < --- (Generate next available number in this column)
paymentinfoid -------------- < --- (Generate next avaliable number in this column)
transactionid --------------- < --- txn_id
state ---------------------- < --- (Set all to "1")
amount -------------------- < --- mc_gross
currency ------------------- < --- mc_currency
dateline -------------------- < --- payment_date
paymentapiid --------------- < --- (Set all to "1")
request -------------------- < --- (Set all to "NULL")
reversed ------------------- < --- (Set all to "0")
To_paymentinfo -----------<
paymentinfoid -------------- < --- (Same generated number that goes to the paymentinfoif field in To_paymenttransaction table )
hash ----------------------- < --- (Set all to "Imported")
subscriptionid --------------- < --- (Set all to "1")
subscriptionsubid ------------ < --- ("2" IF above field mc_gross is 4, "1" IF above field mc_gross is 6, "0" IF above field mc_gross is 10)
userid ---------------------- < --- userid
completed ------------------ < --- (Set all to "0")


The link below also has some sample data using a specific record where data was once moved the opposite way but it might be a bit confusing and the above hopefully has enough information.

https://docs.google.com/spreadsheet/...&rm=full#gid=1

Again, thanks for all the help!
Reply With Quote
  #4  
Old 10-12-2012, 11:38 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK, a couple of things: I think the first move can be done pretty easily with an SQL query, but in the first post you say "It should only update but not overwrite if data for that user already exists". To do that I think you'd need to have a key defined that includes userid, or else there won't be anything stopping multiple rows with the same userid (I think - I'm not actually an SQL expert or anything).

For the second one, the generated numbers are a bit of an issue. Normally you'd make the id fields AUTO_INCREMENT in one of the tables, then if you needed the value for another table you'd get the last inserted value (which I could do in php but I don't know how to do in SQL).

I might be able to take a shot at a php script that handles this. Someone else who's better at SQL than I am might be able to do it all with one or more queries. Also, since this doesn't look like it's vbulletin-specific, you could ask at some place like stackoverflow.com.
Reply With Quote
  #5  
Old 10-12-2012, 03:41 PM
kronnos kronnos is offline
 
Join Date: Apr 2006
Posts: 241
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I apologize, I think I actually do want it to overwrite values (especially the "expirydate -------------- < ----- etime"). However, like you suggested, the key for the first move should be userid because that value has to be unique. So for example if a row already exists with that userid, it should just update the rest of the field and not create a totally new entry with a duplicate userid in the "To_subscriptionlog" table.

I agree that the second move looks a lot more complicated. The only common field in "To_paymentinfo" and "To_paymenttransaction" is paymentinfoid. Would it be possible or easier to somehow create the 2 tables from the 1 independently of each other?:

e.g.

To_paymentinfo -----------< --- From_payments
paymentinfoid -------------- < --- (Generate next avaliable number in this column)
hash ----------------------- < --- (Set all to "Imported")
subscriptionid --------------- < --- (Set all to "1")
subscriptionsubid ------------ < --- ("2" IF above field mc_gross is 4, "1" IF above field mc_gross is 6, "0" IF above field mc_gross is 10)
userid ---------------------- < --- userid
completed ------------------ < --- (Set all to "0")


And once the above is created:

To_paymenttransaction --- < --- To_paymentinfo
paymenttransactionid ------- < --- (Generate next available number in this column)
paymentinfoid -------------- < --- paymentinfoid
transactionid --------------- < --- txn_id field from the "From_payments" table record where userid=userid from "To_paymentinfo"
state ---------------------- < --- (Set all to "1")
amount -------------------- < --- mc_gross field from the "From_payments" table record where userid=userid from "To_paymentinfo"
currency ------------------- < --- mc_currency field from the "From_payments" table record where userid=userid from "To_paymentinfo"
dateline -------------------- < --- payment_date field from the "From_payments" table record where userid=userid from "To_paymentinfo"
paymentapiid --------------- < --- (Set all to "1")
request -------------------- < --- (Set all to "NULL")
reversed ------------------- < --- (Set all to "0")

Its just a suggestion and I dont even know if its possible to perform such statements as above (like VLOOKUP) and reference other tables but I really don?t know much SQL besides the basic queries.

Thank you for looking in to this and all help is much appreciated.
Reply With Quote
  #6  
Old 10-14-2012, 12:25 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Well, for the first one I think you could do this:

Code:
REPLACE INTO To_subscriptionlog (subscriptionlogid, subscriptionid, userid, pusergroupid, status, regdate, expirydate, importsubscriptionlogid)
   SELECT id, subid, userid, termid, active, stime, etime, 0 FROM From_subscriptions_data
but I haven't tested it. That will replace lines with duplicate keys. If you wanted to ignore lines with duplicate keys you could change REPLACE to INSERT IGNORE. Edit: but without any "order by" on the select you don't know which order you'll be inserting the records, so if you have rows in From_subscriptions_data that would result in duplicate keys in To_subscriptionlog, you probably need to figure out a better way to handle it.


If afraid the rest of it is beyond my SQL ability. Hopefully someone else will help.
Reply With Quote
  #7  
Old 10-15-2012, 03:01 AM
kronnos kronnos is offline
 
Join Date: Apr 2006
Posts: 241
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

That's, this looks good!

I just had some other feedback on a possible workaround to the second import.

Right before doing the migration, create a query that would add a "paymentinfoid" to the original table. This way the second import can simply be treated as 2 separate imports.

Unfortunately, I was not give any examples of how the actual queries would look.

How would I create an additional colum that would AUTO_INCREMENT from a number I specify (After i check for last number in "To_paymentinfo" table)?

Also, do do this:
subscriptionsubid ------------ < --- ("2" IF above field mc_gross is 4, "1" IF above field mc_gross is 6, "0" IF above field mc_gross is 10)

I would probably end up running 3 different queries based on the number in the mc_gross field. Would I be able to create a query that says "Only run IF that field value is x?
Reply With Quote
  #8  
Old 10-15-2012, 09:45 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kronnos View Post
How would I create an additional colum that would AUTO_INCREMENT from a number I specify (After i check for last number in "To_paymentinfo" table)?
You can do this to change the next value of an autoincrement column:
Code:
ALTER TABLE tablename AUTO_INCREMENT=X

Quote:
Also, do do this:
subscriptionsubid ------------ < --- ("2" IF above field mc_gross is 4, "1" IF above field mc_gross is 6, "0" IF above field mc_gross is 10)

I would probably end up running 3 different queries based on the number in the mc_gross field. Would I be able to create a query that says "Only run IF that field value is x?
You could use CASE:
Code:
CASE mc_gross WHEN 4 THEN 2 WHEN 6 THEN 1 ELSE 0 END

I think that can be used in place of mc_gross when listing columns in a SELECT.
Reply With Quote
  #9  
Old 10-17-2012, 01:24 AM
kronnos kronnos is offline
 
Join Date: Apr 2006
Posts: 241
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:54 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04786 seconds
  • Memory Usage 2,258KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_code
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (1)post_thanks_box_bit
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (9)post_thanks_postbit_info
  • (9)postbit
  • (9)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete