-2

I am trying to select random post ID's from multiple tables but assuming the user has not already viewed the post before. I have gathered all the post ID's the user has view in an array and I was hoping to use that to rule out a random post.

My thinking has brought me to a query like this but it's obviously wrong because the ID is being used twice.

    (SELECT id FROM table1)
UNION (SELECT id FROM table2)
WHERE id != 423812240250 
ORDER BY rand() 
LIMIT 1
Tiny
  • 209
  • 1
  • 6
  • 18
  • Please elaborate upon _"My thinking has brought me to a query like this but it's obviously wrong because the ID is being used twice."_ – Treffynnon Jan 12 '12 at 12:32
  • It's unclear what you are trying to do and what the != operator has to do with it. – Assaf Karmon Jan 12 '12 at 12:33
  • if you're looking to select a random row of your table, look at this post: [http://stackoverflow.com/questions/19412/...](http://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql) – Gabriel GM Jan 12 '12 at 12:34
  • 1
    `mysql not equal to` <- This query in Google search might've helped you reach the skies! ;) – Shomz Jan 12 '12 at 12:35

1 Answers1

0

Use <> for that:

(SELECT id FROM table1)
UNION (SELECT id FROM table2)
WHERE id <> 423812240250 
ORDER BY rand() 
LIMIT 1
Treffynnon
  • 21,365
  • 6
  • 65
  • 98
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162