Pregunta

Tengo el siguiente código que utilizo para insertar datos de formulario en una sola tabla en mi base de datos.

function insert_interests($uid, $interests) {
/* first, we'll delete any entries this user already has in the table */
    purge_lookup("jss_users_interests_table", $uid);
/* now create the sql insert query */
    global $db;
    $db->query(create_checkbox_query($interests, "jss_users_interests_table", $uid));
}
/* helper function for insert_interests(). removes all rows in $table with $uid */
function purge_lookup($table, $uid) {
    global $db;
    $db->query("DELETE FROM $table WHERE users_id = '".$db->escape($uid)."'");
}
/* helper function for insert_interests(). generates the actual SQL query */
function create_checkbox_query($arr, $table, $uid) {
    $q = "INSERT INTO $table (users_id, subcategories_id) VALUES";
    foreach ($arr as $check) {
    $q .=  " ( '$uid' , $check )" . ",";
    }
/* remove the last comma and return */  
return substr($q, 0, -1);
}`

Inmediatamente después de este código, me gustaría usar los mismos datos de formulario emparejados con otros datos en otra tabla para insertar un nuevo registro en una tabla diferente. Aquí está la estructura para ambas tablas.

jss_users_interests_table

  • users_id
  • subcategories_id

jss_info_requests_table

  • users_id
  • provider_id
  • subcategories_id

jss_providers_assignments_table

  • provider_id
  • subcategories_id

Entonces, lo que debo hacer después de insertar los datos en la jss_users_interests_interests_table es insertar los mismos datos junto con cada subcategories_id's correspondiente provider_id del jss_provider_assignment_table en la jss_info_requests_table . ¿Tener sentido? ¿Lo estoy complicando y complicando?

Cualquier ayuda con la sintaxis sería increíble. Gracias!

¿Fue útil?

Solución

Este SQL podría funcionar.

 INSERT INTO jss_info_requests_table 
 (users_id, provider_id, subcategories_id)
SELECT a.users_id,
 b.provider_id, b.subcategories_id FROM
 jss_users_interests_table a,
 jss_providers_assignments_table b 
 WHERE a.subcategories_id =
 b.subcategories_id AND a.users_id =
 [USERID]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top