0

I have two tables: users and sub_list.

Table users contain username and table sub_list contains username, subscribed_user.

**table users**
*username:* john,harris,samantha,peter,joe,max

**table sub_list**
*username:* harris, harris, peter 
*subscribed_user:* samantha, john, joe

Let's say my name is harris...

Now I want query to select a random user from table users limited by 1, where harris is not subscribed to that user in table subscribed_user.

So I don't want to get results like: samantha and john because I've already subscribed to them.

This code below doesn't work.

SELECT *
FROM users
WHERE users.username='harris' NOT IN (
   SELECT username
   FROM sub_list)
ORDER BY RAND() DESC
LIMIT 1
dnoeth
  • 59,503
  • 4
  • 39
  • 56
  • Please clarify your table structure (maybe with [SQL Fiddle](http://sqlfiddle.com/)), because from your explanation I can't really make sense of how it is set up. A relational database consists of tables, which contain rows. You posted basically two lists and didn't explain their relationship. :) If you don't like to build a fiddle, maybe try "drawing" your database in ASCII within a code block or something. โ€“ Till Helge Oct 12 '15 at 15:50
  • `SELECT * FROM users WHERE username NOT IN ( SELECT username FROM sub_list WHERE username='harris') ORDER BY RAND() DESC LIMIT 1ยด โ€“ dnoeth Oct 12 '15 at 16:03
  • I hope this could help you. Table structure [link](http://i.imgur.com/1Pn5tiX.png) โ€“ Haris Harris Oct 12 '15 at 16:07

0 Answers0