How to insert data from another table inside a table in one query
#1

Hello,

Here is a part of my code to create an account,
Code:
public FUNCTION_CreatePlayer(playerid)
{
    // part 1 >>
	format(p[VAR_ACC_STRING],STR_MEDIUM,"INSERT INTO `plr_account` (`USER_NAME`, `USER_PASS`) VALUES ('%s', SHA1('%s'))",STOCK_PlayerClientName(playerid),p[VAR_ACC_PASSWORD]);
    mysql_query_nodata(p[VAR_ACC_STRING]);
    // part 2 >>
    format(p[VAR_ACC_STRING],STR_SMALL,"SELECT `USER_ID` FROM `plr_account` WHERE `USER_NAME`= '%s'",STOCK_PlayerClientName(playerid));
	mysql_function_query(VAR_SQL_CONNECTION, p[VAR_ACC_STRING], true, "FUNCTION_ReturnUserID", "i", playerid);
	
	// part 3 >>
    format(p[VAR_ACC_STRING],STR_MEDIUM,"INSERT INTO `plr_house` (`USER_ID`, `USER_NAME`) VALUES (%d, '%s')",p[VAR_ACC_ID],STOCK_PlayerClientName(playerid));
    mysql_query_nodata(p[VAR_ACC_STRING]);
    format(p[VAR_ACC_STRING],STR_MEDIUM,"INSERT INTO `plr_items` (`USER_ID`, `USER_NAME`) VALUES (%d, '%s')",p[VAR_ACC_ID],STOCK_PlayerClientName(playerid));
    mysql_query_nodata(p[VAR_ACC_STRING]);
    format(p[VAR_ACC_STRING],STR_MEDIUM,"INSERT INTO `plr_jobs` (`USER_ID`, `USER_NAME`) VALUES (%d, '%s')",p[VAR_ACC_ID],STOCK_PlayerClientName(playerid));
    mysql_query_nodata(p[VAR_ACC_STRING]);
    format(p[VAR_ACC_STRING],STR_MEDIUM,"INSERT INTO `plr_logs` (`USER_ID`, `USER_NAME`) VALUES (%d, '%s')",p[VAR_ACC_ID],STOCK_PlayerClientName(playerid));
    mysql_query_nodata(p[VAR_ACC_STRING]);
    print("3-");
    p[VAR_ACC_REG_STEP] = 1;
	return 1;
}
It works all fine, except that 'part 2' is slower then 'part 3', so this means that the USER_ID's which will set in the query's at part 3 are not correct because he isn't fininshed with getting the USER_ID from the plr_account table.
I guess because it uses threads.
But the question is,
Can I also replace the query's in part 3 to query's that insert the USER_ID and USER_NAME from the plr_account.
So I can remove part 2.
I know it's possible, but everything I found at the internet is just wierd lol.
Something know how it works?

If you don't understand this , no problem then the question is: How to insert data from another table inside a table in one query.
Reply
#2

Nvm I found it haha, sorry if I wasted someones time.
Solution:
Code:
"INSERT INTO plr_house(`USER_ID`, `USER_NAME`) SELECT `USER_ID`, `USER_NAME` FROM `plr_account`"
Reply
#3

You can also string them together using the semicolon. Semicolons are delimiters for the end of a query
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)