PHP - MYSQL - Messages/Users table join issue - php

I'm using MYSQL to display some data from specific tables... However I have small problem..
I'm trying to build some conversation system (for my learnin purpose)...
I query "database name messages" where I have 2 columns, user_one and user_two. The currently logged in user id can be eather user_one or user two (This fields are id's of users)...
So I use SELECT ... FROM table_name WHERE user_one = $id OR user_two = $id); and that display me messages properly where user_one or user_two is currently logged in user. It works fine...
However now I need to know how I join table users and pick only username of "other person", not logged one and display it on each message like "Message from..."!
So lets say I have following:
Users table:
id username
--------------------------
1 username1
2 username2
Messages table:
id title user_one user_two
--------------------------------------------
1 Title 1 2
2 Title2 2 1
So if username1 is logged in, when i output my messages i would like it to output:
Title From
-----------------------------------------
Title username2
Title2 username2
If username2 is logged in then it would say From: username1
So I want to display From username always of second person, not logged in one... I joined table regulary but it display me both users usernames...
Am I doing table structure wrong or how can I do that?
Thanks.

try this
SELECT Messages.title As title ,Users.username as `FROM` FROM Users
INNER JOIN Messages
ON Messages.user_two = Users.id
OR Messages.user_one = Users.id
WHERE Users.username != 'username1'
DEMO

Related

(SQL) Get friends where user and friends are in 2 different tables

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

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

Attempting to list data using ID from a particular table

I have one table which stores user info, including the username.
Another table contains a list of user id's and the user id's of those that they have favorited.
I am trying to figure out the query for listing the usernames of those that user id 1 has favorited.
In my query, assuming that I am uid 1, I need the usernames of uid 3 and 5, but instead
in sqlfiddle I am attempting to join them but I keep getting my username, cjaredrun, instead of the matched usernames for each favorite.
You can see what I have been trying here: http://www.sqlfiddle.com/#!2/c5836/1
Any guidance appreciated
You just need a simple join:
SELECT u.username FROM fav_user f
JOIN users u ON u.uid = f.matchuid
WHERE f.uid = 1
ORDER BY datetime
Fiddle here.
Output:
| USERNAME |
|----------|
| jolet |
| jane |

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)

mysql, php , how to count results that are the same?

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

Categories