mysql query help
#1

Hi

A long time ago i made a mistake by handling house table by playername instead of userid.
Its working, but i want the house table to be dealt with by userid instead.

The problem is that there is alot of houses in the table already and i just added userid.houses field now.

Quote:

Table: houses
Fields: userid, username ..

Quote:

Table: useraccounts
Fields: userid, username ..

So is it possible to make a query that gets username.houses -> SELECT userid.useraccounts -> UPDATE userid.houses to add the correct value from userid.useraccounts to userid.houses ?

I know this can be done with pawn by looping houses -> SELECT userid FROM useraccounts WHERE username = 'HouseOwner' -> then send a new query to UPDATE userid in houses. But i want to know if there is a way this can be done in 1 single query in phpmyadmin.

Thanks in advance.
Reply
#2

Try this:

Код:
UPDATE houses INNER JOIN useraccounts ON houses.username = useraccounts.username SET userid = useraccounts.userid
Reply
#3

You're probably going to need to write a stored procedure with a loop in order to do this. Unfortunately it's been a long time for me too so I can't exactly remember how to do that.
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
You're probably going to need to write a stored procedure with a loop in order to do this. Unfortunately it's been a long time for me too so I can't exactly remember how to do that.
http://www.mysqltutorial.org/mysql-update-join/

I wrote the query above based on this page's content and it worked just fine. I didn't know such thing was possible until now.
Reply
#5

Quote:
Originally Posted by zPain
Посмотреть сообщение
Try this:

Код:
UPDATE houses INNER JOIN useraccounts ON houses.username = useraccounts.username SET userid = useraccounts.userid
This is exactly what i wanted, thank you!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)