Yahiya
Yahiya

Reputation: 791

Join two table from MySQL database

I need to join table accounts and table users from my MySQL database.

$sql = "SELECT id FROM `".DB_ACCOUNTS."` WHERE `id` IS NOT NULL "; 

table accounts and table users contain a same column as id. I want to get gender, birthday from users and bodybuild, weight, height, ethnictype, skincolor from accounts.

Users Table

enter image description here

Accounts table:

enter image description here

How to do this? Please help.

Upvotes: 0

Views: 68

Answers (3)

user8530883
user8530883

Reputation:

Change id column in User table with userid.after that you must add userid at Accounts table as Foreign Key. userid column at Account table contain userid value from User table.

ALTER TABLE Accounts
ADD FOREIGN KEY (userid) REFERENCES Users(userid);

sql = "SELECT u.gender,u.birthday,ac.bodybuild, ac.weight, ac.height, ac.ethnictype, ac.skincolor
       FROM Users u
       LEFT JOIN Accounts ac
       ON u.userid=ac.userid
       WHERE ac.userid IS NOT NULL; ";

enter image description here

Upvotes: 0

Sagar Gangwal
Sagar Gangwal

Reputation: 7937

SELECT US.gender, US.birthday,AC.bodybuild, AC.weight, AC.height, AC.ethnictype, AC.skincolor 
FROM accounts AC
INNER JOIN users US ON AC.ID=US.ID

You can try above code.

It will helps you.

Upvotes: 1

Dharmesh patel
Dharmesh patel

Reputation: 654

$sql = "SELECT u.gender, u.birthday, a.bodybuild, a.weight, a.height, a.ethnictype, a.skincolor 
        FROM accounts a 
        LEFT JOIN users u on a.id = u.id  
        WHERE a.id IS NOT NULL ";

please try above query...

Upvotes: 0

Related Questions