JackAlmighty JackAlmighty - 6 months ago 19
MySQL Question

SQL values from two different tables

Okay so I have two tables in one database. One is called

accounts
and one is called
settings
. Through one query I would like to fetch
settings.positionX
,
settings.positionY
and
accounts.lastlogin
. How would I go about doing this?

Answer

Without knowing what conditions you want all we can do is guess.. But here is what you need to do.

If you want only the records that match in accounts and settings then:

SELECT settings.positionX, settings.positionY, accounts.lastlogin
FROM settings 
INNER JOIN accounts
ON settings.condition = accounts.condition

if you want all the records from accounts (or settings) then join only the matching records then you want a LEFT JOIN / RIGHT JOIN

SELECT settings.positionX, settings.positionY, accounts.lastlogin
FROM settings 
LEFT JOIN accounts
ON settings.condition = accounts.condition

You can see a visual representation of these joins here

Comments