I am a bit new to MySQL and trying my hands on learning it. However I got stuck with an query that goes as follows:
I have 2 tables: Table 1 contains details of lists created by a user. The fields are listid, listname, creatorid, createdat,membercount;
Table 2 stores data of members of each list: The fields are listid, userid;
The query I need to process is as follows: Find out all the pairs of users of the form (u1,u2) where both of the following conditions are satisfied
i. u1 has created at least one list and u2 is a member of that list.
ii. u2 has created at least one list and u1 is a member of that list.
Note: listid in the table 2 is the foreign key for listid in table 1.
How about this?
SELECT l.creatorid AS u1, u.userid AS u2
FROM table2 AS u
INNER JOIN table1 AS l ON l.listid = u.listid
Returns every user from Table2 and the ID of the creator of the corresponding list.
Related
I have 3 tables :
Person table stores basic person wise details with ID as primary Key
This person can have relationships (father / mother etc), which are saved in Relationship table, however the users for them are created in Person table (e.g. ID = 2,3 in person table), This way we know that 2,3 are related to user 1 (carry).
We also have 3rd table - address, which store user ID wise addresses.(for both a user and his related persons, who are also users)
I want to find out if an address exists for either a user or for his related users in SQL. How to achieve this ?
You can combine two rules and search on the combined table as below
SELECT * FROM
(
SELECT username,id,Address.Address
FROM Person
INNER JOIN Address ON Person.id = Address.Userid
UNION ALL
SELECT username,id,Address.Address
FROM Person
INNER JOIN Relationship ON Relationship.Relatedid = Person.id
INNER JOIN Address ON Relationship.Userid = Address.Userid
) as RES
WHERE Address = 'xyz road'
Also you can find DBFiddle link to workout
Query:
select p.id,p.username,(case when a.userid is null then 'No' else 'Yes'end) IsAddressAvailable
from Person p
left join Address a on p.id=a.Userid
Output:
id
username
IsAddressAvailable
1
Carry
Yes
2
Carry-Father
No
3
Carry-Mother
Yes
db<fiddle here
I have three tables, chats, users and chat_user, where chat can have mutiple users and users can have multiple chats. I want to get the chat that is common to all the given user ids.
chats (id)
users (id)
chat_user (id, chat_id, user_id)
I want to get chat that belongs to user 1 and 5 or multiple user ids
Edit
I'm using Laravel framework, so it's better if there's a way to get the result through one of parent tables(users, chats).
But any help would be appreciated.
You may need to join the tables chats (the one that contains the informations you want) and the table chat_user (the one that contains the id of the chat and of the user) to apply where clause on the joined table.
In Laravel, I don't know.
SELECT c.*
FROM `chats` c
JOIN `chat_user` cu
ON c.id = cu.chat_id
WHERE cu.chat_id NOT IN (
SELECT cuu.chat_id
FROM chat_user cuu
WHERE cuu.user_id NOT IN (1, 5))
I am guessing that the userId will be an input parameter here? Try the query below:
SELECT
*
from
chats
WHERE
id in (
SELECT
chat_id
from
chat_user
WHERE
user_id = '1'
OR user_id = '5'
group by
chat_id
having
count(*) = 2
)
The count(*)=2 will ensure only those chats where both user 1 and 5 are present.
I've been using Stack Overflow for years but this is the first time I've not been able to find the answer.
I'm attempting to build a query within MySQL using 4 tables
name, user, admin, actions
the actions table has actions_id, admin_id, user_id and action columns
the user table has user_id and name_id columns
the admin table has admin_id and name_id columns
the name table has name_id, first_name and _last_name columns
what I'd like to do is show the contents of actions replacing the user_id and admin_id with the first_name and last_name for the linking name_id via admin and user
you have to use join to relate all tables together
select t1.action,n.first_name as user_FirstName,n.last_name as user_LastName,
n1.first_name as adminFirstname,n1.last_name as AdminLastName from
(
select a.action,ad.name_id as adnameid,u.name_id as unameid from actions a inner join user u on a.user_id=u.user_id
inner join admin ad on a.admin_id =ad.admin_id
) as t1 inner join name n on t1.unameid =n.name_id
inner join name n1 on t1.adnameid=n1.name_id
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;
A user can have many interests.
An interest can be interested to many users.
My database looks like that:
Users table:
id - primary key,
name,
email,
Interests table:
id - primary key,
title
Users_To_Interests table:
id - primary key,
user_id(id from users table)
interest_id(id from interests table)
How can I improve Users_To_Interests table to be able to pick all users who have the same interest efficiently? user_id and interest_id columns don't have indexes or keys. If I need to add them, please show me how can I make that.
Edition 1: For example,
user1 has interests : interest1, interest2, interest3;
user2 has interests : interest3, interest4;
user3 has interests : interest3, interest5;
user4 has interests : interest4;
If I want to get all users who have interest1, I should receive user1;
If I want to get all users who have interest2, I should receive user1;
If I want to get all users who have interest3, I should receive user1, user2, user3;
The query to get users for interest #3 is very simple (use IN or EXISTS). With an index on users_to_interests(interest_id, user_id) this should be very fast.
select *
from users
where id in (select user_id from users_to_interests where interest_id = 3);
Here is a query which would find all users having interests 1 and 2. It should be clear how to generalize this to any number of interets. The subquery aggregates over users and finds those users who have the interests we want. We then join this back to the Users table to get the full information for each user.
SELECT
t1.*
FROM Users t1
INNER JOIN
(
SELECT ui.user_id
FROM Users_To_Interests ui
INNER JOIN Interests i
ON ui.interest_id = i.id
WHERE i.title IN ('interest2', 'interest3')
GROUP BY ui.user_id
HAVING COUNT(DISTINCT i.id) = 2
) t2
ON t1.id = t2.user_id;