The following query:
SELECT friendship.dom, friendship.sub,
user.id, user.fname, user.lname
FROM friendship
LEFT JOIN user ON friendship.dom = user.id
results in the following:
dom sub id fname lname
---------------------------
2 1 2 Seun Soti
1 2 1 Donal Lynch
But how do I make the output look like:
dom sub id_dom fname_dom lname_dom id_sub fname_sub lname_sub
----------------------------------------------------------------------
2 1 2 Seun Soti 1 Donal Lynch
1 2 1 Donal Lynch 2 Seun Soti
I just need both users (dom and sub) to be included in the output.
How about this:
SELECT
f.dom, f.sub,
d.id dom_id, d.fname dom_fname, d.lname dom_lname,
s.id sub_id, s.fname sub_fname, s.lname sub_lname
FROM friendship f
LEFT JOIN user d ON f.dom = d.id
LEFT JOIN user s ON f.sub = s.id
Try this:
SELECT friendship.dom, friendship.sub, user.id AS id_dom, ...
FROM friendship
LEFT JOIN user ON friendship.dom = user.id
That should work for you. HTH
SELECT friendship.dom, friendship.sub,
user.id, user.fname, user.lname,
u1.id, u1.fname, u1.lname
FROM friendship
FROM friendship
LEFT JOIN user ON friendship.dom = user.id
LEFT JOIN user as u1 ON friendship.sub = u1.id
Related
Here are the two tables:
1.user
user_id | full_name | username
1 A A_1
2 B B_2
3 C C_3
4 D D_4
5 E E_5
2.user_follower
user_id | follower_id | follow_dtm
2 4 2018-10-09 10:10:10
2 3 2018-01-09 11:10:10
1 5 2018-11-09 07:10:10
4 2 2018-10-09 06:10:10
4 5 2018-10-09 00:10:10
Find follower of user: 2
Output should be:
user_id: 4 fullname: D username: D_4 f_total_flwr: 2 (num of flwr of id-4) following: yes
user_id: 3 fullname: C username: C_3 f_total_flwr: 0 (num of flwr of id-3) following: no
I need a mysql query to find all the followers of a particular user with detail information of the followers from user table and need to know the number of followers each follower has and i also need to if the the particular user also following the follower. Here's what I have tried:
SELECT u.user_id
, u.full_name
, u.username
, COUNT(DISTINCT uf.follower_id) f_total_flwr
, case when b.user_id is null then 'no' else 'yes' end following
FROM user_follower
JOIN user u
ON user_follower.follower_id = u.user_id
LEFT
JOIN user_follower uf
ON u.user_id = uf.user_id
LEFT
JOIN user_follower b
ON b.user_id = user_follower.follower_id
and b.user_id = u.user_id
WHERE user_follower.user_id=2
GROUP
BY u.user_id
ORDER
BY uf.follow_dtm DESC
LIMIT 30
I know I'm getting close ;). The problem is, I'm getting following with a yes value even though the user is not following back.Here is another weird thing - not all but some of them showing yes which should be no .Thanks!
Try this:
select u2.*,b.fcount, case when uf3.user_id is null then 'no' else 'yes' end as connected from user u2 inner join
(
select u.user_id,count(distinct(uf2.follower_id)) fcount
from user u
inner join user_follower uf1 on u.user_id=uf1.follower_id and uf1.user_id=1
left join user_follower uf2 on uf2.user_id=uf1.follower_id
group by u.user_id
) b on u2.user_id=b.user_id
left join user_follower uf3 on u2.user_id=uf3.user_id and uf3.follower_id=1
I tried it using the following data sets:
USER
1,a,abcd
2,b,bcde
3,c,cdef
user_follower
1,2,10
1,3,11
2,3,10
3,1,13
And got the expected result:
2,b,bcde,1,no
3,c,cdef,1,yes
I have 3 tables.
Owners:
ownerID name
1 josh
Pets:
petID name
1 M
2 x
3 f
4 h
PetsOwners:
petID ownerID
1 1
3 1
4 1
I have a query that returns the ownerID from a person. "SELECT ownerID FROM Owners WHERE name = 'josh';" This will return ownerID = 1. I need a query that returns all pets that josh owns. In this case will be "m", "f" and "h" according to the petsOwners table.
If you have ownerId use
SELECT p.name
FROM Pets p
JOIN PetsOwners po
ON p.petID = po.petID
WHERE po.ownerID = 1
If you only have the owner name, need join all 3 tables
SELECT p.name
FROM Pets p
JOIN PetsOwners po
ON p.petID = po.petID
JOIN Owners o
ON po.ownerID = o.ownerID
WHERE o.name = 'josh'
If you just want their names:
SELECT Pets.name
FROM Pets, PetsOwners, Owners
WHERE Pets.petID = PetsOwners.petID
AND Owners.ownerID = PetsOwners.ownerID;
try this:
select a.ownerID,a.`name`as OwnerName, b.petID,b.`name` as PetName from
(select ownerID `name` from Owners) as a
right join
(select a.petID,a.`name`,OwnerID from
(select petID,`name` from Pets) as a
left JOIN
(select petID,OwnerID from PetsOwners) as b
on a.petID = b.petID) as b
on a.ownerID = b.OwnerID
I see your question and this is easy you see the query I wrote blow:
SELECT links.`link`,
links.`link_id`
FROM links
WHERE links.`link_id` NOT IN
(SELECT Y.`link_id`
FROM users X
INNER JOIN user_visited Y ON X.`user_id` = Y.`user_id`
WHERE X.`user_id` = 22 );
I have 3 table, log, member, also guest, but my log i stored as customer(user)'s id only, which is either their guest_id or member_id. So here's the problem, because they're from different table, I'm not sure how to join & group together their data.
checkout_log table
id user_id checkout_as
--------------------------------------
1 1 member
2 2 guest
members table
id fullname
--------------------------------------
1 member01
2 member02
guests table
id fullname
--------------------------------------
1 guest01
2 guest02
What I wanted to Achieve - Result
id user_id fullname checkout_as
----------------------------------------------
1 1 member01 member
2 2 guest02 guest
Had tried following sql statement with UNION ALL, or GROUP BY , but had no luck.
SELECT * FROM
(
SELECT checkout_log.id,checkout_log.user_id,guests.fullname,guests.email,checkout_log.checkout_as
FROM checkout_log
LEFT JOIN checkout_product ON checkout_product.checkout_log_id = checkout_log.id
LEFT JOIN guests ON checkout_log.user_id = guests.id
UNION ALL
SELECT checkout_log.id,checkout_log.user_id,members.fullname,members.email,checkout_log.checkout_as
FROM checkout_log
LEFT JOIN checkout_product ON checkout_product.checkout_log_id = checkout_log.id
LEFT JOIN members ON checkout_log.user_id = members.id
) derivedTable
GROUP BY id
Try doing this with joins instead of union
select cl.id, cl.user_id,
coalesce(m.fullname, g.fullname) as fullname,
cl.checkout_as
from checkout_log cl left join
members m
on cl.user_id = m.id and cl.checkout_as = 'member' left join
guests g
on cl.user_id = g.id and cl.checkout_as = 'guest';
I have two tables:
Users:
id name isSpecial
1 Tal 1
2 Jorden 0
3 John 1
4 Paige 0
Details:
id userId Country zipCode
1 1 Israel 4564
2 3 US 554654
I want to get all the data from Users by the name of Jorden OR if isSpecial is 1 to be shown like this
Result:
id name Country zipCode
1 Tal Israel 4564
2 Jorden
3 John US 554654
I know it's supposed to be a simple query but I can't get the results that I want!
You can use a LEFT JOIN:
SELECT
u.id,
u.name,
u.isSpecial,
d.country,
d.zipCode
FROM Users u
LEFT JOIN Detals d
ON u.id = d.userId
WHERE u.name = 'Jorden'
OR u.isSpecial = 1
Like this
SELECT u.id,u.name,d.country,d.zipCode FROM
Users u
LEFT outer JOIN Details d
ON u.id = d.userId
WHERE u.name = 'Jorden' OR u.isSpecial = '1'
Left join would serve your purpose.
SELECT uObj.ID,uObj.Name,DObj.country, DObj.zipcode FROM users uObj LEFT JOIN Details DObj ON uObj.ID=DObj.userID WHERE uObj.name = 'Jorden'
OR uObj.isSpecial = 1;
Please go through the link below
http://www.w3schools.com/sql/sql_join_left.asp
Hope this helps you out.
First of all apologies for the newbee question, I am totally new to MySQL.
I have 3 tables with some fields. The relevant fields for the query are as follows:
table 1 - Registration
user_name
user_email
ID
table 2 - photogallery
ID
user_id
Status
photo_url
table 3 - photovote
Photo_ID
status
I need a list of all the votes (1 record per vote) with explicited data of the photo and the user owning that photo. This means that Photo_ID in photovote is = to ID in photogallery and user_id in photogallery is = ID in Registration.
What I came up with is
SELECT *
FROM photovote
LEFT JOIN (photogallery, registration)
ON photovote.Status = 1
AND photogallery.Status = 1
WHERE photovote.user_id = photogallery.user_id
AND photogallery.user_id = registration.ID
But it really does'nt do the job. Anyone can point me to the right direction maybe?
Thanks a lot.
Andrea
SELECT *
FROM photovote v
INNER JOIN photogallery g ON v.PhotoId = g.ID
INNER JOIN registration r ON g.user_id = r.ID
WHERE v.Status = 1
AND g.Status = 1
SELECT r.user_name, r.user_email, p.status, p.photo_url, pv.status as pv_status FROM registration r LEFT JOIN photogallery p ON (r.id=p.id) LEFT JOIN photovote pv ON (pv.id=p.id) WHERE p.status = 1 AND pv_status = 1