Mysql join 2 table with multiple where clause - mysql

am covering my recently post
fetch results using join Mysql
i made some tries. on wat i want
TABLE users
USERID NAMES EMAIL
1 KAT 1#1.com
2 JOHN 2#1.com
3 FIK 3#1.com
4 PET 4#1.com
5 COW 5#1.com
TABLE comunity
COMUNITY_ID USERID FANS
1 1 KAT #FROM TABLE USERS 3#1.com # FIK IS MY FAN
2 2 JOHN #FROM TABLE USERS 1#1.com # AM FAN OF JOHN
3 1 KAT #FROM TABLE USERS 5#1.com # COW IS MY FAN
4 4 PET #FROM TABLE USERS 1#1.com # AM FAN OF PET
5 1 KAT #FROM TABLE USERS 2#1.com # JOHN IS MY FAN
i want to get all fans which have my email in fans column
i get them like this - this works
SELECT * FROM
users u
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c.FANS = 'MY USERID - 1'
now the problem is here
i want to also get users which i have they emails to fans column
i did it like this
select fans where my userid = '1'
so i need some correction here
SELECT * FROM
users u
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c.FANS = 'MY USERID - 1'
AND * IN(SELECT c.FANS FROM comunity WHERE c.USERID = 'MY USERID - 1");

You can try this Query:
SELECT * from
(
SELECT * from community where USERID=1 OR FANS='1#1.com'
) c
left join
users u
ON u.USERID = c.USERID;

Related

How to get name and surname from two id linked to same table SQL

I have i question about JOINign i have tables User and Category and Request
Id
name
surname
1
Alex
Morgan
2
Tom
Brady
3
Smith
Rowe
Category
Id
Category
1
Party
2
Football match
And now someone create request for another
Request
Id_request
Id_caller
Id_receiver
id_category
1
1
2
1
2
1
3
2
Now i want query to find all request where user 1 (Alex) is caller and get this result
Id_request
Caller_name
Caller_surname
Receiver_name
Receiver _surname
Category
1
Alex
Morgan
Tom
Brady
Party
2
Alex
Morgan
Smith
Rowe
Football match
I try with JOIN but i didn't get this result. Pls help.
You just need to join the tables and take the necessary columns.
However, you need to join the table user twice. When joining a table twice, at least one of them needs to have an alias. The query shown below uses aliases for all tables.
For example, you can do:
select
q.id_request,
c.name as caller_name,
c.surname as caller_surname,
r.name as receiver_name,
r.surname as receiver_surname,
c.category
from request q
join user c on c.id = q.id_caller
join user r on r.id = q.id_receiver -- joined user again with different alias
join category y on y.id = q.id_category
where q.id_caller = 1
You just need two joins, one for the caller and another for the receiver.
select
id_request,
uc.nm_user as "Caller_name",
uc.nm_surname as "Caller_surname",
rr.nm_user as "Receiver_name",
rr.nm_surname as "Receiver_surname",
c.nm_category
from
usr uc
join
request rc on rc.id_caller = uc.id_user
join
usr rr on rr.id_user = rc.id_receiver
join
category c on c.id_category = rc.id_category
where
uc.id_user = 1
Here is the complete example
https://www.db-fiddle.com/f/cA637bx33SFMeDtyUUNqsp/0
You need to join the User table twice with request table -
SELECT R.Id_request,
U1.Caller_name,
U1.Caller_surname,
U2.Receiver_name,
U2.Receiver_surname,
C.Category
FROM Request R
JOIN User U1 ON R.Id_caller = U1.Id
JOIN User U2 ON R.Id_receiver = U2.Id
JOIN Category C ON r.id_category = C.Id;

Inner join table that have to be used two times for different columns

I have table users:
id name
----- -----
1 Mark
2 John
3 Paul
4 Dave
5 Chris
and table matches:
id sender receiver matched
-- ------ -------- -------
1 2 5 2
2 1 3 0
3 1 2 0
So John have sent 1 request for match and his match is accepted.
Also John have receive 1 match which is still not accepted.
I'm using this code to find the names of the users by their ids
SELECT matches.sender as sender,matches.receiver as receiver,users.name as `userName`,
users.user as `userName2` FROM `matches`
INNER JOIN `users` ON matches.sender = users.id
WHERE '$id' IN (sender,receiver) AND matched='2'
but it's pulling out only sender's userName . When I type
...ON matches.sender = users.id AND matches.receiver = users.id ....
the result is 0
So I need to pull out the names of the 2 people.
I think you want two joins:
select m.*, us.name as sender_name, ur.name as receiver_name
from matches m join
users us
on m.sender = us.id join
users ur
on m.receiver = ur.id
where 2 in (m.sender, m.receiver) and
m.matched = 2;

How to fix Sql query

Please i have been trying to get a query to return the friends a user has,in my members table, i have user id ,firstname and lastname of the member, i have user id column and friend id column in the friends table, now i am combining my members table with friends table so i can get the name of a friends.
users table
user_id firstname lastname
2 John drake
3 Hamer Joy
4 Finter Richy
friends table
friends_id user_id friend_id
1 2 3
2 4 2
3 4 3
here is the query i executed
SELECT a.friends_id,a.user_id,
a.friend_id, b.firstname, b.lastname
FROM friends AS a,users As b
WHERE (a.friend_id = b.user_id OR a.user_id = b.user_id) AND
(a.friend_id = 2 OR a.user_id =2)
here is the result i am getting
friends_id user_id friend_id firstname lastname
1 2 3 John drake
1 2 3 Hamer Joy
3 4 2 John drake
3 4 2 Finter Richy
This is the result i am expecting
friends_id user_id friend_id firstname lastname
1 2 3 Hamer Joy
3 4 2 Finter Richy
perhaps take a union of the specified individual's friends plus the users who have him as a friend:
select a.friends_id, a.user_id, a.friend_id, b.firstname, b.lastname
from friends a, users b
where a.user_id = 2 and b.user_id = a.friend_id
union
select a.friends_id, a.user_id, a.friend_id, b.firstname, b.lastname
from friends a, users b
where a.friend_id = 2 and b.user_id = a.user_id
This is all a single query resulting from the union of 2 inner queries.
I think the problem with your original query is that you are using an OR condition in your joins. Normally OR's in join conditions are not helpful.
Here is another solution that is a bit shorter and uses ANSI join syntax so you can clearly see the projections. Disclaimer, I haven't tested it in a database, so there may be an error. However, you can also access the original user name as well if you like (I've added that as the last two columns).
select F.friends_id, U.user_id, FR.user_id,
FR.firstname, FR.lastname,
U.firstname, U.lastname
FROM users U join Friends F on
U.user_id = F.user_id
JOIN Users FR on
F.friend_id = FR.user_id
where U.user_id = 2 or FR.user_id = 2
;
As a general rule, using the ANSI join syntax makes your query much easier to read and helps to ensure all of the join conditions are specified. It also gives you easy access to more join types.

fetch results using join Mysql

MY USERID IS 1
TABLE users
USERID NAMES EMAIL
1 KAT 1#1.com
2 JOHN 2#2.com
3 FIK 3#3.com
TABLE comunity
COMUNITY_ID USERID MYFANS
1 1#FROM TABLE USERS 1#1.com
2 2#FROM TABLE USERS 1#2.com
3 3#FROM TABLE USERS 1#3.com
SELECT u.USERID, u.NAME FROM
users u
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c. MYFANS = '1#1.com'
with his example i only get users which have my email in column MYFANS. but i want to get also users which i have their emails in MYFANS Column
its like select myfans where my email is '1#1.com' and these emails which i have in myfans where my userid = '1'

How to get different results from one mysql table?

I have a user table in the database where all users of the system are stored.
The table has a user_id and a business_name and a first_name.
Some users are merchants and get a business name,
some users are consumers and get a first name.
In a second table I have transactions with a user_id and a merchant_id (which are defining the transaction) and an amount. Both ids reference to user table.
Table users:
user_id bus_name first_name role_id
1 Thomas 10
2 comp1 7
3 Peter 10
4 comp2 7
(role_id is defining with 10=consumer, 7=merchant)
Table transactions:
trans_id amount user_id merchant_id
1 12 1 2
2 23 3 2
3 34 3 4
4 19 1 4
Now I want to have a query with a result as one table:
This table should contain the transaction with amount, user_id, first_name, merchant_id and bus_name.
I want to get this result:
trans_id amount user_id first_name merchant_id bus_name
1 12 1 Thomas 2 comp1
2 23 3 Peter 2 comp1
3 34 3 Peter 4 comp2
4 19 1 Thomas 4 comp2
I have the problem that either I get only the first_name and empty bus_name or I get only the bus_name but empty first_name.
I am using a left join:
...
left join `users`
on(
(`transactions`.`user_id` = `users`.`user_id`)
)
...
But for this I would get for user_id=1 the first_name=Thomas and the bus_name='' would be empty because I only reference to one line in table and not also to different user with user_id=2.
But I want to say something like:
for trans_id=1
get first_name FROM users WHERE transactions.user_id = users.user_id
AND
get bus_name FROM users WHERE transactions.merchant_id = users.user_id
Thanks for your help, I tried so many things but it does not work.
You have to join the user table twice:
SELECT t.*, u.first_name, m.bus_name
FROM transactions t
JOIN users as u
ON t.user_id = u.user_id
JOIN users as m
ON t.merchant_id = m.merchant_id
you could use a duoble join in users table
select a.trans_id, a.amount , a.user_id, b.first_name, a.merchant_id, c. bus_name
from transactions a
inner join users b on a.user_id = b.user_id and b.role_id = 10
inner join users c on a.merchant_id = c.user_id and c.role_id = 7
To join the user table twice worked fine. With "left join users as consumer" I create a kind of a virtual users table called "consumer", this one is joined. Of course in select I had to adjust table name as well. Same for second "virtual" table od users, called "merchant".
select
`transactions`.`trans_id` AS `trans_id`,
`transactions`.`merchant_id` AS `merchant_id`,
`merchant`.`bus_name` AS `bus_name`,
`transactions`.`user_id` AS `user_id`,
`consumer`.`first_name` AS `first_name`,
`cards`.`card_id` AS `card_id`,
`cards`.`serial_no` AS `serial_no`
from (
`transactions`
left join `cards`
on(
(`cards`.`card_id` = `transactions`.`card_id`)
)
left join `users` as consumer
on(
(`consumer`.`user_id` = `transactions`.`user_id`)
)
left join `users` as merchant
on(
(`merchant`.`user_id` = `transactions`.`merchant_id`)
)
)