duplicated column name - mysql

I have 3 tables
users
tickets
Activity
with relationship 1:n ---<-
users 1:n ticket (users can create many tickets)
ticket 1:n Activity (A tickets could have many activities)
Users 1:n Activity (A user can create many Activities that belong to a ticket)
I want to write a query that gives me
username ! ticket ! username ! activities
I try using Inner join but I only have the iduser (PK), and I need the column names.
I don't know how can I difference both names from users table. the first name was the user who create the ticket and the other the user who create the activity and both could be different.

try using alias.
select user.username as user_username, activities.username as act_username from ...

join on the users table twice, once with tickets, once with activities
select u1.username, a.ticket, u2.username, b.activity
from tickets a
join activities b on a.ticketid = b.ticketid
join users u1 on a.userid = u1.userid
join users u2 on b.userid = u2.userid

Related

Multiple joins off same table

I'm trying to connect two different types of users from the same users data table.
I have a database table with the following schema
agent_relationships
buyer_id
agent_id
I have a table with user data:
users
id
first name
last name
What i want is a data set that has the following:
buyer id first name
buyer id last name
agent id first name
agent id last name
I know im doing this incorrectly, but heres what I've attempted:
This one returns the same data for both buyer and agent, which i expected
select u1.first_name as buyerfirst,u1.last_name as buyerlast,u1.first_name as agentfirst,u1.last_name as agentlast from users u1 left join agent_relationships ar1 on ar1.buyer_id=u1.id left join agent_relationships ar2 on ar2.agent_id=u1.id
This one fails
select u1.first_name as buyerfirst,u1.last_name as buyerlast,u2.first_name as agentfirst,u2.last_name as agentlast from users u1,users u2 left join agent_relationships ar1 on ar1.buyer_id=u1.id left join agent_relationships ar2 on ar2.agent_id=u2.id
select BU.first_name, BU.last_name, AU.first_name, AU.last_name
from agent_relationships
left join users BU on buyer_id = BU.id
left join users AU on agent_id= AU.id
You need two join on user .. depending from agent_relationships
select u1.first_name as buyerfirst
,u1.last_name as buyerlast
,u2.first_name as agentfirst
,u2.last_name as agentlast
from agent_relationships ar
left join users u1 ar.buyer_id=u1.id
left join users u2 ar.agent_id=u1.id

Join between two tables on multiple column

I am wondering if I can have join between two tables but on different columns? Let me explain because it is different with most of the cases that I've seen...
I have a table for all the messages between users and each user has a unique user id. so In the first table I have:
Tx User Id .......... Rx. User Id .......... Date ............ Message
and in user tables I have
user Id .............. User name
Can I have a join query that gives me
Tx User "Name" ........... Rx. User "Name: ....... Date ....... Message
The problem is that in my join apparently I can only define
SELECT messages.* users.name
FROM messages JOIN
users
ON messages.RxId = users.id OR messages.TxId = users.id
which is only 1 field, but as I explained above I need 2 field as Rx user name and Tx. User Name based on which id in my messages table is matched.
Thanks a lot in advanced.
You want two joins. And for this you need to learn about table aliases (a good thing):
SELECT m.*, urx.name, utx.name
FROM messages m LEFT JOIN
users urx
ON m.RxId = urs.id LEFT JOIN
users utx
ON m.TxId = utx.id;

Three way join with linking table

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

MySQL - Get count of users that have a "user-contact"

Consider the three tables in the following Schema SQL Fiddle.
This mini-model represents a simple example of a database I'm working on .
The Users table has 4 attributes, the user_id (primary key auto_increment), the country_id which is a foreign key referencing the Country table, the user_msisdn which is the phone number of the user, and the username.
The Contacts table has 3 attributes, the contact_id (primary key auto_increment), the user_id which is a foreign key referencing the Users table, and the contact_msisdn which is the user contact's phone numbers (the phone numbers on your phone contacts list).
The relation between the Users table and the Contacts table is many-to-many, a user can have many contacts, and a contact can be found in any user's contacts list.
Requirements:
For each country, get the count of users that have at least one "user-contact", where a "user-contact" is a contact that is a user, and the count of users that don't have any "user-contact".
E.g., For country = 'Sweden' (country_id = 3), the user_id = 3 has two contacts in the Contacts table that are considered "user-contact" with (msisdn = '+220011223344' & '+224433221100'). So the query results that I want: Sweden has 1 user (user_id = 3) that has at least one "user-contact" and zero users that have no "user-contact", and so on for each country.
Try this:
SELECT c.country_id,
COUNT(DISTINCT CASE WHEN u2.user_id IS NOT NULL THEN u.user_id END) as has_contact_that_is_user ,
COUNT(distinct CASE WHEN u2.user_id is null AND u.user_id IS NOT NULL THEN u.user_id END) as has_no_contact_that_is_user
FROM Country c
LEFT JOIN users u
ON(c.country_id = u.country_id)
LEFT JOIN contacts co
ON(co.user_id = u.user_id)
LEFT JOIN Users u2
ON(u2.user_msisdn = co.contact_msisdn)
GROUP BY c.country_id
Fiddle

mysql query help to find mutual friends on social networking site

I have a database table called users with a primary key of user_id for each user.
I also have a table called friends with two fields, user_id and friend_user_id.
The user_id field is always the lowest of the two user_id's in order to avoid duplicate entries.
Say I have two users in mind, (lets say user id 1 and user id 4 although they could be anything).
How would I return all rows from the users table for users that are friends with user 1 and user 4 (i.e mutual friends)?
I will give you the recipe:
Find all friends of user 1
Find all friends of user 2
Intersect them and the result will be the mutual friends.
Much like this:
UPDATE: Here's the query:
select f.friend_user_id from friends f where f.friend_user_id in (
select friend_user_id from friends where user_id=<id_of_user_a>)
and f.user_id=<id_of_user_b>
The ids returned by above query will be the id of all the users that are mutual friends of user_a and user_b. If you want to get all the details (name, etc) about those users, then do this:
select f.friend_user_id,u.* from friends f inner join users u
on u.user_id=f.friend_user_id
where f.friend_user_id in (
select friend_user_id from friends where user_id=<id_of_user_a>)
and f.user_id=<id_of_user_b>
SELECT friends.friend_user_id FROM user, friends
INNER JOIN friends ON friends.user_id = user.user_id
WHERE user.user_id = 1
AND friend.friend_user_id
IN (SELECT friends.friend_user_id
FROM user, friends
INNER JOIN friends ON friends.user_id = user.user_id
WHERE user_id = 4)