Dave
Dave

Reputation: 97

1 Notification for all users

I have a notifications system and I want the user to have 1 notification by us when they log in. when a user interacts with another it uses notification_target user and that target user gets the notification by id.

So.. I'm wondering if I set a notification up in the table is there anything I can put in notification_targetuser table.. ALL USERS or maybe 1 to 1000 so all thousand members get that one notification.

so instead it just being targetuser 15, I would want it to be all users from 1 to 15 if possible.

Upvotes: 1

Views: 168

Answers (1)

JohnLBevan
JohnLBevan

Reputation: 24410

You could use a null value for the "for all" indicator, using coalesce to ensure the result is returned for any user.

Sadly SQL Fiddle's down at present, and I don't have a copy of MySQL. Here's an example for MS-SQL:

declare @table table(
id bigint not null identity(1,1)
, targetUserId bigint
, [notification] nvarchar(256)
)

insert @table
select 5, 'notification to user 5'
union select 6, 'notification to user 6'
union select null, 'notification to all users'
union select 11, 'notification to user 11'
union select 12, 'notification to user 12'

--get notifications for user 1 (there are none, except the one for all)
declare @targetUserId bigint = 1
select * from @table where @targetUserId = coalesce(targetUserId,@targetUserId)

--get notifications for user 5 (one result for id=5 plus one for all)
set @targetUserId = 5
select * from @table where @targetUserId = coalesce(targetUserId,@targetUserId)

Upvotes: 1

Related Questions