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

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

Related

notifications alerts turn off sql 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

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).

count repeated value in table field

how i can count the repeated value in any field of mysql db's table.
Example:
id name
1 aaa
2 aaa
3 ttt
4 ccc
5 ttt
6 ccc
7 aaa
8 zzz
How i can get how many times value is repeated in table like.
aaa =3 times in table
ttt =2 times in table
ccc =2 times in table
zzz =1 times in table
I think it possible with count of the mysql but how to use it i dont know any one can help ?please answer ma question thanks in adv.
You need to group by your name column and then you can use an aggregate function like count() on that group
select name, count(id)
from your_table
group by name
Write below query to get count of each name
select name, count(name)
from your_table
group by name
OR
to get count of specific name
select name, count(name)
from your_table
where name = "aaa"
group by name

MySQL Table Joins

I have a table which links the userID and the friendID (which is another user) to say they are friends.
If User 1 adds User 24 as their friend, the first row in the below table will happen
If User 10 adds User 1 as their friend, the second row in the below table will happen
From there, even if User 1 is on either side, they are friends with the adjacent ID.
FRIENDS TABLE:
userID | friendID
-----------------
1 | 24
10 | 1
What I need to do now is: If User 1 is logged in, I need to display the people they are friends with. This query I have wrote:
SELECT DISTINCT (Users.username), friends.userID, friends.friendID FROM Users, friends WHERE Users.userID IN(SELECT userID FROM friends WHERE Users.userID = friends.userID) OR Users.userID IN(SELECT DISTINCT(userID) FROM friends WHERE Users.userID = friends.friendID)
Will bring back:
username | userID | friendID
-----------------------------
name1 | 10 | 1
name1 | 1 | 24
name2 | 10 | 1
name2 | 1 | 24
name1 = ID 1
name2 = ID 10
Is there a way where I can retrieve the logged in Users'friends (e.g. if ID #1 is logged in) whether they appear in the userID or friendID columns. and then, in this case, retrieve ID 24 and 10 because they are linked with ID #1, and then when it comes to displaying, eliminate ID # 1 from the list because it will bring up that they are friends with themselves.
Hope this makes sense and thank you in advance!
SELECT DISTINCT s.FriendId
FROM (
SELECT f.FriendId
FROM Friends f
WHERE f.UserId = #id
UNION ALL
SELECT f.UserId
FROM Friends f
WHERE f.FriendId = #id
) s
WHERE s.FriendId != #id
You may not need the last WHERE given that a user probably is not adding himself or herself as a friend.
While the way you are storing data may seem efficient, the following may be a better approach:
When a MUTUAL friendship is formed, enter the data twice: once as userID(1) friendID(24) and once as userID(24) friendID(1). The reason this approach may be good is that it makes your table reusable.
Presently your table is like facebook: if I am your friend then perforce you are also my friend: I see your activities; you see my activities. The design I explain allow you to use the table as both facebook and twitter: just because you are following my activities does not mean I want to follow yours.
Here's an easy way to do it with UNION. This also takes care of duplicates:
(SELECT userID
FROM friends
WHERE friendID = 1)
UNION
(SELECT friendID
FROM friends
WHERE userID = 1)
If you want to return usernames too, you can do your JOINs inside the subqueries:
(SELECT f.userID, u.username
FROM friends f
JOIN users u
ON u.userID = f.userID
WHERE f.friendID = 1)
UNION
(SELECT f.friendID, u.username
FROM friends f
JOIN users u
ON u.userID = f.friendID
WHERE f.userID = 1)

Count users from table 2 in request to table 1

I have table with positions
tbl_positions
id position
1 Driver
2 Lobby
3 Support
4 Constructor
and in other table i have users
tbl_workers
id name position
1 John 2
2 Mike 3
3 Kate 2
4 Andy 1
i do request of positions
SELECT position FROM tbl_positions
but i also need to show how many workers are assigned to each position i tried to do separate request
SELECT id FROM tbl_workers WHERE position = 2
but cannot display all together in table cannot bind number of users to position.
How can i make join this queries into one, so it also show positions without workers assigned?
join and group by
SELECT p.id, p.position, count(*) FROM tbl_positions as p
inner join tbl_workers as w on w.position=p.id
group by p.id, p.position

Categories