I have 3 tables for a chat app
chatRooms (id)
chatMessages (id, roomId, userId, message)
userData (userId, nickName)
How can I load all the chatRooms with the chatMessages with the nickNames?
I tried:
$qry = $db->prepare('SELECT
chatRooms.id,
chatMessages.message,
userData.nickName
FROM chatRooms
LEFT JOIN chatMessages
ON chatMessages.roomId = chatRooms.id
LEFT JOIN userData
ON chatMessages.userId = userData.userId '
);
$qry->execute();
But it doesn't seem to work for me:C
I would like to display all the users who are in the chatRoom in the chat name
So if 3 people (Fred, Joe, Bane) are in the group then I somehow would need an array with them. For every array element(chatRoom)
Your first LEFT JOIN is more than likely returning more than one row.
From your question, your "chatMessages" table also has a userId in it. You should also filter by this.
In order to do this, you would need to:
Join userData before chatMessages
Include the userId from userData in the LEFT JOIN on chatMessages
$qry = $db->prepare('SELECT
chatRooms.id,
chatMessages.message,
userData.nickName
FROM chatRooms
LEFT JOIN userData
ON chatMessages.userId = userData.userId
LEFT JOIN chatMessages
ON chatMessages.roomId = chatRooms.id AND chatMessages.userId = userData.userId');
$qry->execute();
Related
I'm doing a favorite system which allow users to save the posts to their page.
The tables I'm using are:
saved_posts table which contain 3 columns (painned_id``user_id``post_id)
users which contain (user_id, frist_name, last_name, username, email, password, user_website, user_avatar)
Finally posts table (post_id, user_id, post_author, category_id, post_date, post_image, post_avatar, user_website, post_keywords, post_content)
The my sql code I'm using to get the saved posrt
SELECT * FROM posts INNER JOIN saved_posts
ON saved_posts.post_id =
posts.post_id INNER JOIN users on saved_posts.user_id = users.user_id WHERE
saved_posts.user_id = users.user_id AND saved_posts.post_id = posts.post_id
The problem is that the saved posts from one user appears for other user as if they saved them as well.
Shouldn't you filter your answer with a WHERE condition? Otherwise it is just finding all posts that any user shared EVER.
SELECT * FROM posts
INNER JOIN saved_posts
ON saved_posts.post_id = posts.post_id
INNER JOIN users
ON saved_posts.user_id = users.user_id
WHERE users.user_id = :wanted_user_id
Don't forget to bind wanted_user_id.
If I understood the question correctly, the task is to select all the posts which of the users
SELECT * FROM posts p
INNER JOIN saved_posts sp ON p.post_id = sp.post_id
and sp.user_id = u.user_id
INNER JOIN users u on p.user_id = u.user_id
My database has 3 tables i wish to access in the select query but I cannot seem to get it to work. Selecting from 2 tables works fine so I know everything else is working apart from my code for selecting from 3 tables. My database has been created on PHPmyadmin
The Tables are as follows:
forum_replies
reply_id
topic_id
user_id
reply_text
reply date
forum_topics
topic_id
category_id
user_id
topic_title
topic_description
topic_date
users
user_id
username
This is the code I have tried to use and shows the fields I wish to select:
$queryreply = "SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date
FROM forum_replies
JOIN forum_topics
ON forum_replies.topic_id = forum_topics.topic_id
JOIN users
ON forum_replies.user_id = users.user_id
";
$result = mysql_query($queryreply) or die (mysql_error());
$row = mysql_fetch_array($result);
Example in code would be appreciated. Thanks
Use this query:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
Apart from syntax errors, you should use LEFT JOIN and table alias in your case.
To show also the topic creator's username, you can adjust the query to the following:
SELECT a.reply_text, a.reply_date, b.topic_title, c.username AS reply_user, (SELECT username FROM users WHERE user_id=b.user_id) AS topic_creator
FROM forum_replies a
LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
LEFT JOIN users c ON a.user_id=c.user_id
// apply WHERE, ORDER, GROUP if needed
You miss the , after users.username..
SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username,
forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date
I have a join that I have working perfectly. However, I need to join to a third table called wp_users so I can access the user display name. When I add a second join I keep getting all sorts of errors depending on what I try so I'm turning for help.
The third table, wp_users has two columns:
wp_users.ID,
wp_users.display_name
Here is the code before I add the second join code so I can get the display name for the results ID
//Works, but I need to join to wp_users on wp_users.ID to get display_name
$results = $dbh->prepare("select
stories.ID,
stories.SID,
stories.story_name,
stories.category,
points.ID,
points.PID,
FROM stories
JOIN points ON stories.SID=points.SID
JOIN stories ON wp_users.ID=points.ID
where (points.ID = $user_ID) and (PID = 1)");
$results->execute();
Change stories to wp_users
JOIN stories ON wp_users.ID=points.ID
to
JOIN wp_users ON wp_users.ID=points.ID
$results = $dbh->prepare("select
stories.ID,
stories.SID,
stories.story_name,
stories.category,
points.ID,
points.PID,
FROM stories
JOIN points ON stories.SID=points.SID
JOIN wp_users ON wp_users.ID=points.ID
where (points.ID = $user_ID) and (PID = 1)");
$results->execute();
I'm trying to make a query that returns the following:
All users such that:
-They are not an admin or owner account
-They have the same client_id as the project's client_id
-They are not already in the project_users table with entry project_users.project_id = 9
Here is my MySQL query:
SELECT `users`.`id` as id, `users`.`first_name` as first_name, `users`.`last_name` as last_name, `users`.`username` as username
FROM (`users`)
JOIN `projects` ON `projects`.`client_id` = `users`.`client_id` AND projects.id = 9
LEFT OUTER JOIN `project_users` ON `users`.`id` = `project_users`.`user_id`
WHERE `users`.`user_type` != 'Admin'
AND `users`.`user_type` != 'Owner'
For some reason, this query seems to return all non-super(not owner or admin) users with the same client_id as the project, but does NOT exclude those already in the project_users table (ie. the LEFT OUTER JOIN statement isn't working).
Can anyone tell me what is wrong with the query?
Thanks!
You need to add a filter to find the rows that don't match. Also, your query can be helped by using table aliases:
SELECT u.`id` as id, u.`first_name` as first_name, u.`last_name` as last_name, u.`username` as username
FROM `users` u JOIN
`projects` p
ON p.`client_id` = u.`client_id` AND p.id = 9 LEFT OUTER JOIN
`project_users` pu
ON u.`id` = pu.`user_id`
WHERE u.`user_type` not in ('Admin', 'Owner') and
pu.user_id is NULL;
I have a user table, e.g.
userId
userName
and I have a message table, e.g.
messageId
messageToId
messageFromId
messageContent
I am trying to make a query to pull a message, but also get the user names from the user table based on the messageToId and messageFromId.
I have done this before with only 1 field between tables, e.g.
SELECT message.*, user.userName
FROM message, user
WHERE user.userId = message.messageToId
AND messageId = (whatever)
But I am having trouble with 2 links.
I want the result as follows:
messageId
messageToId
toUserName
messageFromId
fromUserName
messageContent
Any help would be much appreciated, or if someone had another way of attempting a private message system with PHP/MySQL.
You just have to use joins and different table aliases:
SELECT m.*, u1.userName AS toUserName, u2.username AS fromUserName
FROM message m INNER JOIN user u1 ON m.messageToId = u1.userId
INNER JOIN user u2 ON m.messageFromId = u2.userId
WHERE messageId = "XXX";
You need to use a join from to achieve this:
SELECT `m`.*,
`to`.`userName` AS `to`,
`from`.`userName` AS `from`,
FROM `message` `m`
JOIN `user` `to` ON `m`.`messageToId` = `to`.`userId`
JOIN `user` `from` ON `m`.`messageFromId` = `from`.`userId`
WHERE `m`.`messageId` = 1
So you join against the user table twice to get both users for a particular message. To do this you need to use table aliases as I have done with to and from so that you distinguish between them.
I have also used a field alias to get their usernames separately eg:
`to`.`username` AS `from`
Will this work?
SELECT b.userName AS author, c.userName AS reciever, a.messageId, a.messageContent FROM message a JOIN user b ON a.messageFromId = b.userId JOIN user c ON a.messageToId = c.userId