i have a table that has users and the pages that they visited.
i might have user user1 that visited pages: index.php 3 times and 'test.php' 4 times.
then user user2 that visited pages: index.php 6 times and 'test.php' 10 times.
what i want to do is to display the most visited pages by user in order.
any ideas?
thanks
edit: table schema. table name findme
talentnum | page_name
user1 | test.php
user1 | test.php
user1 | index.php
user2 | test.php
user3 | index.php
Something like this:
SELECT
TALENTNUM,
PAGE_NAME,
COUNT(*) AS CNT
FROM FINDME
GROUP BY TALENTNUM, PAGE_NAME
ORDER BY CNT DESC
w/o knowing your table schema:
SELECT user, page, count(*)
FROM users u
JOIN page p
ON u.user_id = p.user_id
GROUP BY user, page
ORDER BY user, page DESC;
Whenever a user loads a page insert a row into the table to record his ID and the Page visited. You can then run an SQL query
SELECT talentnum,page_name,COUNT(page_name) FROM findme
GROUP BY talentnum,page_name
ORDER BY COUNT(page_name) DESC
Related
I've got 2 tables: users & friends. I want to show each profile picture of the friends of the logged user, but loggedUser-friends match is in a different table (which is friends) than the one that contains the URI to the profile pictures (which is users).
My query now looks like this:
SELECT profile_picture FROM users WHERE
And now I have to complete the WHERE clause:
friend's table schema is the following:
id | user_id | friend_id
friends table's sample data:
1 | pino20 | sebo14
while users' table schema is
id | user_id | profile_picture
users table's sample data:
1 | pino20 | pino20/profile.jpg
With below query you get your frends userid and their profile picture.
select user_id , profile_picture from users WHERE user_id in( select friend_id from friends where user_id=$your_userid)
Above result can be achieved using join
SELECT NT.profile_picture FROM users AS NT JOIN friends AS FR ON FR.id = NT.id
You can achieve this by using joins. It should look like this:
EDIT:
SELECT user.profile_picture,user_friend.profile_picture
FROM user
JOIN friend ON user.user_id = friend.user_id
left join user user_friend on user_friend.friend_id=friend.user_id -- left join since we don't want to drop records if there is no match
I have two tables in my MySql database:
user
- sid
- userid
- username
log
- sid
- userid
- login_time
As you can guess, there's a lot more records in log tables than in user table.
I am using php to present these records on my website in a table format as shown below.
no | userid | username | number of login |
1 | inzo | harvey | 233 |
2 | chae | schmidts | 433 |
3 | hibro | swainy | 12 |
To get the number of login for each user, I can send another queries in a for statement. But it's consuming resources and making the server slow in the end.
Can I have this result in one single join query?
Yes you can, you have to use count the logins for each user with a group by
select t1.userid, t1.username, count(t2.sid)
from user t1
left join
log t2
on t1.userid = t2.userid
group by t1.userid, t1.username
The left join ensures you that users without logins will still be returned, wit 0 as count.
Edit
About the question in the comment: if you want to only count the logins with a specific flag value, you can just add where flag = x before the group by; if you want to have a separate count for each value of the flag, you have to add that flag to both group by and select.
I guess best and by that I mean least resource consuming way would be to add "number_of_login" to user table and just increase it every time he/she is logged in, because any other solution will require looping
SELECT TABLE_A.row_id, TABLE_A.category, TABLE_A.val_1, TABLE_B.val_2
FROM TABLE_B
LEFT OUTER JOIN TABLE_A ON TABLE_B.row_id = TABLE_A.row_id
ORDER BY row_id;
If you want all the results, you need an outer join, not an inner one.
select a.sid,a.userid,a.username,COUNT(b.sid) from user a
Left join log b ON b.sid =a.sid group by a.sid
I am using Laravel to creating a website, my users can post questions and other users can write their comments under the post, each comment have Up vote and Down vote, and users can voting for comments.
I need most liked (Up vote) shows topper than others..
This is my database structure and I join them together:
comment table:
comment_id | question_id | user_id | timestamp
up and down votes table (like):
like_id | comment_id | like_type | user_id | timestamp
note:like_type is an enum on mysql and its values are upvote and downvote.
1-What is the mysql query and Laravel codes for that?
2-Is my database Structure right?
1.Calculate SUM belongs to Each Comment
2.Make it order by Desc
select * from (
select ct.comment_id,ct.question_id,
ct.user_id,SUM(case when vt.like_type='upvote' then 1 else -1 end )
cnt from commenttable ct
from votestable
vt left join
on
vt.comment_id=ct.comment_id
)D order by D.cnt desc
so we have:
table users
id name password parent_id
the first user have the id 1, and others have the parent_id 1, so I select all the users that have the parent_id == 1 - they are the childs of the user with 1, okay its all right, but now i need to select the users that have the parent_id of the selected before users with they id, if they exists of course
user with id 1
/ | \
/ | \
/ | \
users with parent_id 1
user id 2 user id 3 id user 4
| | |
| | |
| | |
and here is the same, I need to select all the users that have the parent_id 2, 3, 4 for each of those user, its is something like a pyramide(triangle) from the top to the bottom
So the question is how can i make a query that will select it in one shot, not in many queries by extracting the id and then make other query - its not good i think
do you have an idea??
Here is a question, that covers your problem:
Is it possible to query a tree structure table in MySQL in a single query, to any depth?
Query below works only for finding children and grand-children of a single user and is a product of misunderstanding the question!
You could try joining user table on itself twice.
SELECT * FROM users as up
JOIN users as u on up.id=u.parent_id
JOIN users as uc on u.id=uc.parent_id
WHERE up.id={$grandParentUserId}
Aliases: up = user's parent, u = user, us = user's child.
Definitely not a pretty solution, but it's a single request.
I see you are using CI. You can have a look at this answer. Somewhat related to your question. You can select the users with NULL parent ID first and then populate their children
https://stackoverflow.com/a/9937130/876117
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)