Reece Costello Reece Costello - 13 days ago 5
MySQL Question

2 SELECT in 1 query results in 4 results when expecting 2 results

I have 2 tables in mysql database: accounts and users

Accounts looks like this:enter image description here

Users Looks like this:enter image description here

I want to select accountid and userid from both tables and get results like so:

accountid || userid
7 22
29 44


I don't understand why it gives me 4 results like so:

enter image description here

I'm using this syntax:

SELECT accounts.accountid, users.userid
FROM accounts
INNER JOIN users


I rewrote my question because it was poorly written, sorry guys!

Answer

According to your screenshots you have tables linked by username which is very poor db design.

So you should definitely change your schema and use user.id to link your tables.

But so far your query is:

SELECT accounts.accountid, users.userid
FROM accounts
LEFT JOIN users
ON accounts.username=users.username
Comments