notifications alerts turn off sql php - php

I'm creating a basic notification system to alert an user, that the users he follows have created a new post.
Users
id_user | name
1 Max
2 Joe
3 Ed
4 Tommy
Posts
id_post | id_user_post | posts
1 2 hi
2 2 hello
3 2 how are you
4 3 hey you
5 2 how long
6 1 whats up
7 2 come on
Community
id_follower id_followed
3 2
3 1
4 2
In this case Ed (user 3) follows to Joe (2) and Max (1), they both, have posted 6 posts.
SELECT COUNT(*)
FROM community c
LEFT JOIN posts p ON p.id_user_post=c.id_followed
WHERE c.id_follower=3
Here is how it looked like in the page
Homepage header
You have (6 new posts) > [click here to see]
My problem is how do I turn off the notification alert (6 new posts) after clicking on the it?
Do I need to create a notifications table? Should I need to add an status field to the post?
Do I need to make an Sql query again? Otherwise that notification is going to appear forever.

You should add a last_post_id column to the community table. Then you can count only the posts whose ID is higher than this.
SELECT COUNT(*)
FROM community c
LEFT JOIN posts p ON p.id_user_post=c.id_followed AND p.id_post > c.last_post_id
WHERE c.id_follower=3
Whenever you show the status to a user, you update the last_post_id to the highest ID:
UPDATE community AS c
JOIN (SELECT id_user_post, MAX(id_post) AS id_post
FROM posts
GROUP BY id_user_post) AS p ON p.id_user_post=c.id_followed
SET c.last_post_id = p.id_post
WHERE c.id_follower = 3

Related

How to call back a parents name from child table?

I have two tables as follows:
games
id | game_name | console_id
1 God of War 1
2 Zelda 3
3 Sonic 4
consoles
id | console_name
1 PS4
2 Xbox
3 Switch
4 Mega Drive
I've linked them via the Designer view so when I go to insert a name game (I'm doing this via the phpMyAdmin control panel) as soon as I click on "console_id" it gives me a drop down of 1 - PS4, 2 - Xbox and so on. So the games table can now read from consoles table no problem so I think I've got everything correct from that side of things.
Now I can run this code
SELECT id, game_name, console_id FROM games
and I will get the output
1 - God of war - 1
What I would like to do is something like this:
SELECT id, game_name, console_id.console_name FROM games
So instead of it saying God of war is on console ID 1, it says its on console.name PS4.
How can I do it, as I thought by linking the tables it would allow me to do it.
You need a JOIN between the tables. In this case, you want to join the consoles table to the games table by matching the id field of the former to the console_id field of the latter, like this:
SELECT g.id, g.game_name, c.console_name
FROM games g
INNER JOIN consoles c
ON c.id = g.console_id

Select rows where one value in field name occured in multiple rows

Suppose I have a database structure like this.
Table users
id name
1 Name1
2 Name2
3 Name3
4 Name4
And another table logs
id userid
1 1
2 1
3 2
4 3
5 3
6 3
7 4
8 4
9 4
Now if I enter 3 in text box and press submit then in the server side PHP I need to fetch and display records from users table with id 3 and 4 i.e., Name3 and Name4 from users table as in logs table they occurred 3 times each. Similarly, if I press 2 and submit it should fetch and display record Name1 as id 1 from users table has occurred 2 times in logs table. What should be the query here for this? I don't understand how to start with this so I cannot post what I have tried. Please help.
Try this query, it is working
select name from users where id in (select userid from logs group by userid having count(userid) = '".$inputvalue."')
DEMO LINK
Group by users and take only those groups having the count of records you input
select u.name
from users u
join logs l on l.userid = u.id
group by u.name
having count(l.id) = $inputValue
Demo
This will work for you.
Select u.name
from ( select userid,count(*) as NoOfRecords
from logs
group by userid
having count(*) = $inputvalue ) as a
inner join users u on a.userid = u.id

Simple MySQL Two Table Query [duplicate]

This question already has answers here:
MySQL join with where clause
(3 answers)
Closed 5 years ago.
I have two tables
1st table :-
id name dept
1 John dept1
2 Mary dept2
3 Dave dept3
4 John dept4
5 John dept5
2nd table :-
id submitter dept
1 Rupert dept3
2 Joe dept1
3 Lisa dept2
4 Louise dept4
5 Tom dept5
what i would like is a query to allow people in the name column in the first table to only show records based on their matching departments eg John in table one will return the 3 records in table 2 (id 2,4 and 5)
So far i have tried SELECT * FROM table1, table2 WHERE table1.dept = table2.dept AND table1.name='John'
If I correctly understand your problem, you need to make a join between the two tables using the field dept and filter your results by the name of the requester, in the first table.
SELECT t2.submitter, t2.dept
FROM table1 t1
LEFT JOIN table2 t2 ON t1.dept = t2.dept
WHERE t1.name = :person_name
Documentation and examples (you can also look at the left menu at Inner, right, full and self join's).

Display friend post according to the friend connected to each other?

I have 2 tables in mysql frnd_request and nsm_post i want to show the post according to the user share post to primary school mate , secondary school mate etc.
frnd_request table referece is the column from which user are connected to each other.
table structure below
1 id
2 user_id
3 frnd_id
4 status
5 referece (how friend are connected to each other like primay, secondary school mate)
6 confirmation
7 note
8 date
nsm_post post table show_to is the field which define which friend group of logged in user can see this post.
1 id
2 uid (user id of user who posted this post)
3 post
4 ip
5 show_to (to which friend group post will be display)
6 Img_video_name
7 date
SELECT nsm_post.post
FROM frnd_request, nsm_post
WHERE frnd_request.user_id = nsm_post.uid
This should work.
I guess what you are trying to do is restricting the posts to the user's school mates. But this is not really possible with your current table structure.
You have to setup an additional table like this:
primary | uid1 | uid2 | type
0 | 10004 | 10005 | school mate

Count how many times a value appears in sql query using php

I have created a database and website that will be used by football managers to select their team etc. Once a match has been completed events will be stored in the match_players table. Such events are Goal, Yellow Card, Red Card etc. I have no problem getting this information into php from SQL db.
I need to add up how many times a Goal appears (a '1' is placed in the SQL table) and for what team so that a final score can be displayed. So, for example, if Team A has 1 goal and Team B has 2 then I need to display that. I am trying to count the amount of times that a Goal is registered in the table. Any help will be greatly appreciated.
You can use MYSQL SUM
select SUM(Goal) from match_players where Team="A"
Or you can get the same for all teams by
select Team,SUM(Goal) from match_players group by Team
Why don't you demand this sum to SQL directly?
SELECT SUM(goals)
FROM match_table
WHERE team = 'Barcellona'
This should be much faster also than elaborate all data at "php-level"
If you want this detail for all teams
SELECT team,SUM(goals)
FROM match_table
GROUP BY team
Well if you store a 1 each time a goal is scored, your table looks like this:
TeamID goal
1 1
2 1
1 1
3 1
2 1
2 1
1 1
So you just want a count of how many times a team appears in that table:
select TeamID, count(*) from table group by TeamID
Will give you
TeamID | count(*)
1 | 3
2 | 3
3 | 1

Categories