Jump to content

Archived

This topic is now archived and is closed to further replies.

boonamera

inversing a selection when joining tables

Recommended Posts

table 1 (users)
userid
username

table 2(payments)
paymentid
userid

I need two things
USERIDS of those who PAID. This works:
SELECT u.userid FROM users u JOIN payments ON u.userid = payments.userid;

USERIDS of those who haven't paid. This is my problem
I tried something along the lines of: but no luck
SELECT u.userid FROM users u LEFT JOIN payments ON u.userid = payments.userid LIMIT 99999 OFFSET ("SELECT ( (SELECT COUNT(*) FROM users)-(SELECT COUNT(*) FROM payments) ));
but no luck

anyone have an idea?

Share this post


Link to post
Share on other sites
If people who haven't paid don't have an entry in the payments table, then the following should work.
[code]
SELECT
u.userid
FROM
users AS u
LEFT JOIN
payments
ON
u.userid = payments.userid
WHERE
payments.userid IS NULL
[/code]

Share this post


Link to post
Share on other sites
Yeah, go with shoz's suggestion... I nearly fell of my chair when I saw those crazy subselects with that LIMIT clause. ;-)

Share this post


Link to post
Share on other sites
AWESOME IT WORKED
thanks shoz

lol hey I used the minimal knowledge to try and get it working :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.