Select from two tables statment - mysql

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.

Related

Mysql query for finding followers and following

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

MySql query on 3 different tables

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 );

MYSQL - Union SELECT & GROUP BY Not working

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';

Get two usernames from the same team

Below is the query I am using to get the team information. However, I want also display both usernames assorted with the team. The database is setup using two tables. One contains all of the user information and the other (userLeagues) contains a set of ids, such as userID and teamID.
Example - userLeagues
leagueID | userID | teamID
1 1 1
1 5 1
1 10 2
2 1 1
2 8 1
This is result I am trying to achieve.
leagueID | TeamID | userID1| userID1
1 1 1 5
This would then be outputed as :
leagueID | TeamID | userID1 | userID1
1 Mad Racing driver 1 dirver 5
This is the code I have so far which gets the team but how do I get both driers and their username associated with the same team?
SELECT
u.username
, t.teamName
, t.teamImage
/* File Table */
, f.fileType
FROM
userleague ul
INNER JOIN users u ON u.userID = ul.userID
LEFT JOIN teams t ON t.teamID = ul.teamID
LEFT JOIN fileType f ON f.fileID = t.fileID
WHERE leagueID = 1
GROUP BY ul.teamID
You can do this with min() and max(), if there are only two values:
SELECT t.teamName, t.teamImage, f.fileType,
min(u.username) as user1,
(case when max(u.username) > min(u.username) then max(u.username) end) as user2
FROM userleague ul INNER JOIN
users u
ON u.userID = ul.userID LEFT JOIN
teams t
ON t.teamID = ul.teamID LEFT JOIN
fileType f
ON f.fileID = t.fileID
WHERE leagueID = 1
GROUP BY t.teamName, t.teamImage, f.fileType;

MySQL - How do I achieve my desired output with LEFT JOIN?

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