Jump to content

Select from table if id doesn't exist on another table


arcdigital

Recommended Posts

I've got 2 tables

content (id-int, uid-int, catid-int, contenttext-text)

usedblocks (id-int, userid-int, contentid-int)

 

I want to select all the blocks where category id = X that don't exist on usedblocks where userid=X

Here is what I got so far...

select contenttext from content left join usedblocks on content.id=usedblocks.contentid where usedblocks.userid CAN'T FIGURE THIS OUT AND content.categoryid = 1 order by rand();

 

I want to select where usedblocks.userid != 1, but that doesn't work...

 

Can anyone help me? Thanks!

I've tried both, Here is the exact query that failed.

 

I'm doing something wrong, because when I do

select contenttext from content left join usedblocks on content.id=usedblocks.contentid where usedblocks.userid <> 1 AND content.categoryid = 1 order by rand() limit 1

 

I don't get what I want (I get nothing).

If I have

category table --  (id=1, name=category1)

content table -- (id=1, categoryid=1, text=AAA)  AND  (id=2, categoryid=1, text=BBB) AND (id=3, categoryid=1, text=CCC)

usedblocks table -- (id=1, userid=1, contentid=2)

 

If I run the query on category 1 for userid 1, I should get the content AAA and CCC (BBB isn't included, since it's in the usedblocks table for user 1)

You have:

 

where usedblocks.userid  1

 

And in your sample data you have:

 

usedblocks table -- (id=1, userid=1, contentid=2)

 

which won't return anything cause you don't have a userid from this table where it's not 1...

 

Archived

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

×
×
  • Create New...

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.