Laravel Mysql statement like fcm topic condition - mysql

I'm trying to make my own topics system similar to fcm on mysql.The table is simply this:
user_id topic_name
In fcm for example:
'topic1' in topics && 'topic2' in topics or ('topic3' in topics)
means select the user, which is subscribed to topic1 and topic2 or is subscribed to topic3.
In mysql I have no idea how to do this,basically it's based on all equavelnt user_ids but on different conditions on topic_name but using whereIn and whereNotIn and stuff it's not possible I'm really confused.

I suspect that you want aggregation and a having clause:
select user_id
from mytable
where topic_name in ('topic1', 'topic2', 'topic3')
group by user_id
having count(*) = 3
This brings users that have the three topics listed in the where clause.

Related

Randomize a winner pair from the subscriptions

I have a project, where customer needs a winnner pair for events. The users of this site can "like" other user's (just like on FB), they subscribe to a particular post, and the script will generate a winner pair from the subscribers.
So I need a SQL query to randomize a winner pair from the list of pairs, where the users liked each other, and subscribed to a particular post.
How do i do that ?
I cant write a query that, because i got unexpected results.
I have 3 tables : events, likes, subs (and users ofc)
events table : event_id, event_name
subs table: sub_id, event_id, uid
likes table: liker, liked (the two uid from the users table)
Now I can make pairs from the likes table (i self-joined the table where liker = liked AND liked = liker) and randomized, but how can I join the subs and the events tables to the likes table to achieve that a randomized pair will be a subscribed users for a particular event too ?
My current query looks like this :
SELECT L.liked AS T1, L.liker AS T2
FROM likes AS L, likes AS K
WHERE L.liked = K.liker
AND L.liker = K.liked
ORDER BY rand( )
LIMIT 0 , 1
I googled everything about joins for one week, but i cant achieve that.

Find the one-time users in a system- sql table

I have a table with 6 columns- Date, time, action, user_id, channel, and time_and_date.
Action refers to open or close, when a user starts or end watching a tv channel.
My tasks are as following
to get an overview of the data:
- find the one-time users (who used the service only once or in only one day and
never came back) for each channel, each genre, each community
Anoother table provides the user_id, genre(news, sport....)
How can I find the one time users for those requirements?
You can try something like
SELECT FROM first_table LEFT JOIN users ON first_table.user_id=users.id GROUP BY users.id HAVING COUNT(users.id)=1
You can join your genre table after for selecting over genre channels...
To get the one-time users:
select user_id ,min(channel) as channel, min(genre) as genre, min(community) as community
from action_table
group by user_id
having min(date) = max(date);
Note the having clause. This guarantees that a users has only one date (but not necessarily one record).
This returns one value for each of the three dimensions -- for a one-time user they are the same. For someone who visits multiple times in one day, it chooses one value.
Sounds something like this:
select user_id, count(*)
from action_table
where action = 'open'
group by user_id
having count(*) = 1
order by user_id

Mysql COUNT VS num rows performance

I want to select the amount of forum posts compared to a list of users. So it will look like this:
USERID --- FORUMPOSTS
3647 - 2
7467 - 14
2673 - 39
3224 - 5
... and so on
Now I'm asking if it would be faster to count the forum post via COUNT(*) or looping through the userlist before and creating a new query for each user to count his/her forum posts via mysql_num_rows.
You can let SQL do the grouping and counting
select userid, count(*) as forumposts
from your_table
group by userid
Now I'm asking if it would be faster to count the forum post via COUNT(*) or looping through the userlist before and creating a new query for each user to count his/her forum posts via mysql_num_rows.
It'll be faster to do the former, "count the forum post via COUNT(*)". You can group the results as follows:
SELECT userid, COUNT(*) FROM my_table GROUP BY userid
It'll be even faster still if your table has an index on the userid column.

MySQL get most recent events on multiple tables

This might not be possible but would be amazingly awesome if it were. I have the following basic table structure:
groups
group_id
other_stuff
users
user_id
other_stuff
users_to_groups
user_id
group_id
other_stuff
events
event_id
group_id (where the event belongs)
other_stuff
events is actually a set of tables for the various actions a user can perform on the site.
I would like to be able to perform a query on of the tables and have it return a result something like:
event_type event_id info_columns ...
user_join user_to_group_id
photo_upload photo_id
comment comment_id
where the value in the event_type column would be one generated by the query based on the table name of the source content.
I know I can do this using multiple queries and then piecing them together in PHP, but I was thinking that maybe there is a way to do it entirely in MySQL. Is something like this even possible? If so, what are the basic steps to make it happen?
if you have a number of select statements, and you get the data you want in each, then of course you can join them.
Others have asked similar questions, mysql-join-most-recent-matching-record-from-one-table-to-another
Now, given you're only asking for the latest event. your pseudo code goes
select <userinfo>
from users
join groups
on user=group
join (select lastest event by group
from events
) as tmp
on group=tmp
That way you do 1 query, you hand off that work to the database

mysql query with two tables?

I have two tables:
posts(id,user_id,event_id}
events(event_id,name,date]}
I want to make a query, to retrieve all the names of the events for a particular user_id say id number 2.
In pseudo-code lets say
select all the event names from posts where user_id=2
try:
select events.name from posts, events
where posts.event_id = events.event_id and user_id = 2
You will need to have something slightly different depending on how you want results with no matches to display.
select name from events join posts using (event_id) where user_id = 2