简体   繁体   中英

How do I write a query that includes users that aren't in the table at all?

I currently have this query set up:

SELECT age, hobbies, country
FROM profile
INNER JOIN matches
ON profiel.usernumber = matches.user1
WHERE age BETWEEN ? and ?
AND usernumber != ?
AND usernumber NOT IN (SELECT user2 FROM matches WHERE matches.user1 = ?)

FYI: I'm using two tables. Table "profile" has all the user info we need (age, hobbies, etc.). Table "matches" has two user numbers of users that matched. Only if you click the "match" button you get inserted into the "matches" table with your user number and your match.

The problem: The current query only selects users that are in the table "matches" but aren't matched with you. I also want the users that aren't matched at all (and thus aren't in the matches table)

I've tried multiple options with AND or OR but I haven't figured anything out myself. I'm new to coding so I'm grateful for any help.

INNER JOIN only show records that exists on both tables. Use LEFT JOIN instead.

SELECT age, hobbies, country
FROM profile
LEFT JOIN matches ON profiel.usernumber = matches.user1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM