I want to get all user names and last_messages with who the current user had conversation.
I have two tables:
Thread
User
So, if current user's id is 1, I should get rows for bob and mike, but I get empty result. Could you please help me to find the mistake.
SELECT * FROM thread
LEFT JOIN user
ON ((thread.user1_id!=current_user AND thread.user1_id=user.id)
OR (thread.user2_id!=current_user AND thread.user2_id=user.id))
WHERE current_user = 1;
Try this:
select
calls.*
from User
left join (
select
User.Name, thread.user2_id as Partner_id
from User join thread on thread.user1_id = user.id
union all
select
User.Name, thread.user1_id as Partner_id
from User join thread on thread.user2_id = user.id
) calls
on calls.partner_id = User.id
where User.id = 1
Note how the double join from thread to User is done - once against both sides of the call in the subquery as an inner join, then a second time as outer table of an outer join.
Please try in this way
SELECT * FROM thread t
LEFT JOIN user u1
ON t.user1_id = u1.id
LEFT JOIN user u2
ON t.user2_id = u2.id
where t.user1_id = '1' or t.user2_id = '1'
should get rows for bob and mike (Thread row 1 & 2 )
Related
This is what I'd think is a fairly common pattern, but I'm just struggling with the appropiate query for it.
User Table
id
Member table
id
name
User Member link table
user_id
member_id
A user may exist in the user's table, but not have a row in the User Member link table.
I want to select all rows of the User table, and where a user has a link to a member in the user member link table to show the columns linked to them from the member table.
Here's what I've got, but it only gets the rows from the User table that are linked:
SELECT user.id, user.username, member.id, member.name
FROM users
LEFT JOIN user_member ON user.id = user_member.user_id
JOIN member ON user_member.member_id = member.id;
I should get something like this:
user.id user.username member.id member.name
1 bob null null
2 alice 10 Alice
3 jane 11 Jane
4 joe null null
Any suggestions?
I assume a member_id in the user_member table always has a corresponding row in the member table. First, join member and user_member. Second, join user.
SELECT user.id, user.username, member.id, member.name
FROM users
LEFT JOIN
(user_member INNER JOIN member ON user_member.member_id = member.id)
ON user.id = user_member.user_id;
Try using a CROSS JOIN
SELECT user.id, user.username, member.id, member.name
FROM users u
CROSS JOIN member m
LEFT JOIN user_member um
ON u.id= um.user_id
AND m.id= um.member_id
Consider the following :
**Table 1 - record**
id (int primary key),
addedby (int),
editedby (int)
**Table 2 - users**
id (int primary),
name (shorttext)
**Sample Records**
record
0 1 1
1 1 2
users
1 user1
2 user2
What I need is so do a join to to be able to show the following :
record.id, users.addedby, users.editedby
I tried, amongst others, the following :
select record.id, users.name, users.name from record left join users on record.addedby=users.id left join users on record.editedby=users.id
However, it's not even logical that that will work, so I am a bit stuck.
Any help would be much appreciated. Thank you.
Just join the same table twice. Nothing unusual. You just have to alias the tables to be able to refer to them independently.
select r.id, u1.name added, u2.name editor
from record r
inner join user u1
on r.addedby = u1.id
inner join user u2
on r.editedby = u2.id
heres a demo
Use aliasses:
select record.id, users1.name, users2.name
from record
left join users users1 on record.addedby=users1.id
left join users users2 on record.editedby=users2.id
My SQL is only returning one field when it should be returning one for each user.
Any idea where I'm going wrong? If you need additional information I can provide, but I'm just not sure where to go with this at the moment.
Here is my SQL:
SELECT uId, uForename, SUM(biProductPrice * biQuantity) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
GROUP BY uId, uForename
any columns starting with u belong to the users table.
any columns starting with ord belong to the orders table.
any columns starting with b belong to the basket table.
any columns starting with bi belong to the basketitems table.
EDIT:
Everything now works fine except for my SUM, there are only 2 fields with an ordStatus between 4 and 50, so they are the only ones that apply, the biQuantity for one is 8 and the biProductPrice is 100, the other field has a biQuantity of 1 and a biProductPrice of 100, why is it returning a value of 400?
Group by the user and the sum will be returned for each one
SELECT users.id, users.name, SUM(biProductPrice) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
group by users.uId, users.name
SELECT users.id, users.name, SUM(biProductPrice) AS uTotalSpent
FROM users
LEFT JOIN orders ON uId = ordUserId
LEFT JOIN basket ON ordUserId = bUserId
LEFT JOIN basketitems ON bId = biBasketId
WHERE ordStatus BETWEEN 4 AND 50
group by users.uId, users.name
I have a table - comments. Users can post if not a member of the site but want to show their details if they are.
So if a user comments who is NOT a member I show their posts but don't link to their profile, because they don't have one.
So, in the following query I want to return the rows even if there is no join:
select wc.comment, wc.comment_by_name, wc.user_id, u.url from comments wc
join users u on wc.wag_uid = u.user_id
where id = '1237' group by wc.comment order by wc.dateadded desc
I want to return:
comment comment_by_name user_id url
------- --------------- ------- ----
hello dan 12 /dan
hey jane /jane
world jack 10 /jack
But the above does not return the data for jane as she does not have a user_id
Is there a way to return all data even if the join is null?
use LEFT JOIN instead
SELECT wc.comment, wc.comment_by_name, wc.user_id, u.url
FROM comments wc
LEFT JOIN users u
on wc.wag_uid = u.user_id
WHERE id = '1237'
GROUP BY wc.comment
ORDER BY wc.dateadded DESC
basically INNER JOIN only select records which a record from one table has atleast one match on the other table while LEFT JOIN select all rows from the left hand side table (in your case, it's comments) whether it has no match on the other table.
There are plenty of question about join on the same table, but I can't find something related to my problem
I have two tables:
user (id, name)
friends (from, to)
And I have the following query. It is supposed to retrieve all users with their friends :
SELECT user.id, user.name, f.to, friend.id, friend.name
FROM user
LEFT JOIN friends f ON user.id = f.from
LEFT JOIN user friend ON user.id = f.to
LIMIT 0, 200
It returns something like this:
id name from to id name
1 bob 1 3 NULL NULL
1 bob 1 4 NULL NULL
2 toto 2 7 NULL NULL
The from and two are correct, but the second join doesn't seem to work. Do you have any ideas what is wrong with the second join ?
Try this:
SELECT user.id, user.name, f.to, friend.id, friend.name
FROM user
LEFT JOIN friends f ON user.id = f.from
LEFT JOIN user friend ON friend.id = f.to
LIMIT 0, 200
Note that I replaced user with friend in the join condition.