0

I am trying to load notifications for users from a mysql table.

so all the notifications will be saved in a notification table with the unique SQL ID, the sql ID of the user and the notification.

Only the notification with his ID will be showed to the used.

But I also want to let admins do global notifications and I don't want to create for example 1000+ rows just to show one notification to everyone, so I want to have a WHERE clause in the mysql query like

SELECT x FROM y WHERE userID = $id OR global = 1

but what if there are global notifications and user notifications too?

How do I show both? Because in the above statement will probably pick the user notifications and ignore the global notifications.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • "Because in the above statement will probably pick the user notifications and ignore the global notifications" - No. It will return all rows with `userID = $id` and all rows with `global = 1`. However you can also try a UNION statement for a better performance. – Paul Spiegel May 06 '17 at 14:57
  • @PaulSpiegel can you show me a example or link me a tutorial? – Mister Knuckles May 06 '17 at 15:05
  • https://dev.mysql.com/doc/refman/5.7/en/union.html - http://stackoverflow.com/questions/13750475/sql-performance-union-vs-or – Paul Spiegel May 06 '17 at 15:07
  • As I wrote - your query is fine. But `SELECT x FROM y WHERE userID = $id UNION SELECT x FROM y WHERE global = 1` might be faster if both columns are indexed. – Paul Spiegel May 06 '17 at 15:12

0 Answers0