I want to join two tables even if there is no match on the second one.
table user:
uid | name 1 dude1 2 dude2
table account:
uid | accountid | name 1 1 account1
table i want:
uid | username | accountname 1 dude1 account1 2 dude2 NULL
the query i'm trying with:
SELECT user.uid as uid, user.name as username, account.name as accountname FROM user RIGHT JOIN account ON user.uid=accout.uid
what i'm getting:
uid | username | accountname 1 dude1 account1
Yes, you can! The longer answer is yes, there are a few ways to combine two tables without a common column, including CROSS JOIN (Cartesian product) and UNION. The latter is technically not a join but can be handy for merging tables in SQL.
The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.
SELECT B. Accountid FROM TableB AS B LEFT JOIN TableA AS A ON A.ID = B. Accountid WHERE A.ID IS NULL; LEFT JOIN means it takes all the rows from the first table - if there are no matches on the first join condition, the result table columns for table B will be null - that's why it works.
Outer joins are joins that return matched values and unmatched values from either or both tables.
use Left Join
instead
SELECT user.uid as uid, user.name as username, account.name as accountname FROM user LEFT JOIN account ON user.uid=accountid.uid
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With