Keeping this clean and to the point:
I have two MySQL tables, users (id, nick) and bans (id, banned, bannedBy, length).
I want to display in a table a list of bans, but instead of displaying the banned ID and the bannedBy ID, I want to display their nick.
I can use an JOIN to get the nick of one of them, in something like this:
SELECT bans.id,bans.banned,bans.bannedBy,bans.length,users.nick
FROM bans
JOIN users ON users.id=bans.banned
But then I can't get the bannedBy's nick, and vice verca.
I hope I was clear, thanks in advance for any help.
You have to join users table twice, on different keys.
SELECT
bans.id,
bans.banned,
bans.bannedBy,
bans.length,
u.nick as 'banedNick',
u2.nick as 'bannedByNick'
FROM
bans
JOIN
users u ON users.id = bans.banned
JOIN
users u2 ON users.id = bans.bannedBy
You can use two joins:
SELECT bans.id,bans.banned,b.nick as bannedBy,bans.length,u.nick
FROM bans
JOIN users u ON u.users.id=bans.banned
JOIN users b ON b.users.id=bans.bannedBy
Related
I am developing a networking site where I have to show random users profiles (excluding members who are already connected) to logged-in user to connect with. I have one members table which contains fields as memberid,firstname and lastname. I have another table for connections which has fields as memberid and friendid.
Now when I use left join on connections table, I get profiles of only connected members which I dont want. I only want to show profiles form members table which are not connected with logged-in user
You can achieve this without Join with the use of Not in.
select *from
members
where
members.memberid not in (select memberid
from connections
)
;
The above query simply means to display info of memberids that are not in connections.
EDIT:
Since you've already used Left join. here is the query to achieve this task with Left join:
select m.*
from members m
left join connections c
on m.id = c.id
where c.id is null;
Hope it helps!
I believe there is a good way to do. I searched and tried but could not find.
I want to join three tables like this (or a better way, please suggest)
SELECT listing.*, users.username, review.rNumber
FROM listing, users,review
WHERE users.uid=listing.cuid and listing.lid=review.lID
But I don't want the review.rNumber I want to get sum of it like sum(rNumber). Because one listing can have many reviews.. Thank you..
Here is what I want to achieve with this query
All the values from listing table.
only username from users table
sum of reviews from review table
The relation is
users.uid=listing.cuid and listing.lid=review.lID
or
users.uid=listing.cuid=review.sellerID
Please let me know if I need to add table..
Thanks :)
you can get the sum for user with proper group by but for join with all the listing result you could use a dinamic join
SELECT listing.*, t.username, t.sum_rNumber
fFROM listing
INNER JOIN users ON users.uid=listing.cuid
INNER JOIN review ON listing.lid=review.lID
INNER JOIN (
SELECT users.username as username, sum( review.rNumber) sum_rNumber
FROM listing
INNER JOIN users ON users.uid=listing.cuid
INNER JOIN review ON listing.lid=review.lID
GROUP BY users.username
) t on users.username = t.username
(and is more readable the explicit join sintax)
I have to tables in MySQL database, USERS and FRIENDS. They have this structure:
USERS TABLE:
FRIENDS TABLE (two users are friends ONLY if exists two rows like in the image):
I want to create a MySQL query in PHP to SELECT the nombre (name) of the users that are my friends. I read that I have to use the operator LEFT JOIN but I did not succeed.
Thank you!
Try using INNER JOIN instead. Try below query -
SELECT users.nombre FROM users INNER JOIN friends ON
(users.id = friends.usuari_o OR users.id = friends.usuari_t)
WHERE (friends.usuari_o = $id OR friends.usuari_t = $id);
I answer to myself.. IT WORKS! Can I do it efficiently? 3 selects in one query..
SELECT nombre FROM usuarios WHERE id IN
(SELECT usuari_one FROM friends WHERE usuari_two='$id')
AND (SELECT usuari_two FROM friends WHERE usuari_one='$id')
I want to have some help creating my query to get information from three different tables sharing information in common.
My first table is:
auctions
id title description user_id(who posted it)
My second table is:
bids
id user_id bid auction_id owner_id
My third table is:
users
id username X XX XXX XXXX
...and my SQL is as follows however it's not returning any results:
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title FROM auction_bids, auctions
WHERE auctions.user_id=".$_SESSION['userid']."
INNER JOIN users ON auction_bids.user_id = users.id
WHERE auction_bids.owner_id = ".$_SESSION['userid']."
What I need is to capture the auction's title, username who bidded on the auction and the bid. the auction has to have a bid and posted by the user who owns the $_SESSION['userid'].
Any help is appreciated.
You have two different 'where' statements, which may just need combining;
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title FROM auction_bids, auctions
INNER JOIN users ON auction_bids.user_id = users.id
WHERE auction_bids.owner_id = ".$_SESSION['userid']." AND auctions.user_id=".$_SESSION['userid']."
However, I'm not sure this is really what you want, as it will return only records where the specific user both 'owns' the item AND has bidded on it (both based on the userid session), rather than displaying all records from different people who have bidded on an item 'owned' by the user.
Something like: ?
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title FROM auction_bids, auctions
INNER JOIN users ON auction_bids.user_id = users.id,
WHERE auction.owner_id = ".$_SESSION['userid']."
Hope this points you in the right direction!
you have 2 where clauses, that is incorrect. I have revised your query based on your requirements.
SELECT auction_bids.user_id AS applicant, auction_bids.*, auctions.title
FROM auction_bids, auctions
INNER JOIN users ON auction_bids.owner_id = users.id
WHERE auction_bids.owner_id = ".$_SESSION['userid']."
AND auctions.user_id=auctions_bids.owner_id
I need a little help setting up my query. I'm simply trying to access the amount of people who are in the same 'clan' by joining these two tables together, clan, users. Each users has a column 'clan' which is the same as the table clan's column 'roomOwner' and then I'm trying to get the table clan's information along with the amount of members so it would be like: room, roomOwner, members
So basically all I have is this:
SELECT c.*, count(u.clan) AS members FROM clans c inner join users u WHERE c.roomOwner = u.clan ORDER BY members;
It only shows one clan though. Any help please?
Your query has no GROUP BY clause. and I think it's only returning single record right? LEFT JOIN is needed here since there are possibilities that a clan has no member.
SELECT b.roomOwner, COUNT(a.clan) memberCount
FROM clan b
LEFT JOIN users a
ON a.clan = b.roomOwner
GROUP BY b.roomOwner
ORDER BY memberCount
You forgot GROUP BY. Do you have some "id" column in "clans" table? Group by that "id"
SELECT c.*, count(u.clan) AS members
FROM clans c
inner join users u ON c.roomOwner = u.clan
GROUP BY clans.id
And you need LEFT JOIN there instead of INNER JOIN if you want to see info about all clans, even having 0 users.
Perhaps this will help:
select c.*, count(links.id) as members
from clans c
left join users u on c.roomOwner = u,clan
group by u.clan
order by members