Get two usernames from the same team - mysql

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;

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 Select Distinct with Left Join?

I am trying to get a list of company_id's that have no company-level notes. The company may, however, have location-level notes.
company
-------------------------
company_id name deleted
1 Foo 0
2 Bar 0
3 Baz 0
location
-----------------------
location_id company_id
6 1
7 2
8 3
note
-----------------------------------------
note_id company_id location_id deleted
10 2 6 0 // location-level note
11 1 7 0 // location-level note
12 null 8 0 // location-level note
13 2 null 0 // company-level note
I would want my result table to be this:
company_id name
1 Foo
3 Baz
Update
Foo/company_id = 1 does not have a company-level note because the note also has a location_id, which makes it a location-level note. Company-level notes are notes that only link to a company (and not a location).
End of Update
I've tried doing something like this, but it returns an empty set, so I'm not sure if it's working and there aren't any companies without company-level notes or if I'm doing something wrong.
SELECT DISTINCT
c.company_id,
c.name
FROM company AS c
LEFT JOIN note AS n
ON c.company_id = n.company_id
WHERE
c.deleted = 0 AND
n.deleted = 0 AND
n.location_id IS NOT NULL AND
n.location_id != 0 AND
c.company_id = (SELECT MAX(company_id) FROM company)
Revised Accepted Answer by Mike
SELECT
company_id,
name
FROM company
WHERE
deleted = 0 AND
company_id NOT IN (
SELECT DISTINCT
c.company_id
FROM company AS c
INNER JOIN note AS n
ON c.company_id = n.company_id
WHERE (
n.deleted = 0 AND
(n.location_id IS NULL OR
n.location_id = 0)
)
);
The easiest way to think about this is to first find the all the companies that have company level notes, which you can do with
select distinct c.company_id
from company c
inner join notes n
on c.company_id = n.company_id
where n.location_id is null;
Then simply remove these companies from the company select:
select company_id,
name
from company
where company_id not in (select distinct c.company_id
from company c
inner join notes n
on c.company_id = n.company_id
where n.location_id is null);
*Updated to use inner join instead of comma-separated join.
SELECT DISTINCT c.*
FROM company c
LEFT
JOIN note n
ON n.company_id = c.company_id
AND n.location_id IS NULL
WHERE n.note_id IS NULL;

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

Select from two tables statment

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.

Select a grid territory/sales people/category combos where all line items have more than one quantity

Using AdventureWorks 2008 R2, I want to query a grid that
Lists each territory / sales person combination on the vertical axis
Lists each subcategory on the horizontal axis
Each cell specifies the total number of sales (unique SalesOrderID s) where all the line items in that category / territory / sales person combination have 2 or more quantity
Here's a sample (made up data, I don't know how to query the real stuff!):
M.Bikes Chains Gloves
Northwest John Doe 15 4 3
Canada John Doe 4 2 1
Northwest Jill Doe 0 5 3
Canada Jill Doe 1 5 1
etc
I think the following SQL will get me this for mountain bikes (subcat id 1), but I don't know how to add more columns easily. If I start making an independent query for every column, its going to get very slow, very quickly (especially when I have to rejoin all those columns together!)
select
terr.Name as Territory,
sp.BusinessEntityID,
SUM(case when detail.OrderQty > 1 then 1 else 0 end) as MatchingSales
from
sales.salesorderdetail detail
inner join sales.SalesOrderHeader header on header.SalesOrderID = detail.SalesOrderID
inner join sales.SalesPerson sp on sp.BusinessEntityID = header.SalesPersonID
inner join sales.SalesTerritory terr on terr.TerritoryID = sp.TerritoryID
inner join sales.SpecialOfferProduct sop on sop.ProductID = detail.ProductID
inner join Production.Product on Product.ProductID = sop.ProductID
inner join Production.ProductSubcategory subcat on subcat.ProductSubcategoryID = Product.ProductSubcategoryID
where
subcat.ProductSubcategoryID = 1 --mountain bikes
I want to preface this by saying I don't have AdventureWorks installed so I can't Check the query... I also don't know what the sales person name column is called so you will more than likely have to change that but here you go, you will just have to add a column at the top for each sub category. I am also assuming the original query is correct.
select
terr.Name as Territory,
sp.Name SalesPerson,
SUM(case when detail.OrderQty > 1 and subcat.ProductSubcategoryID = 1 then 1 else 0 end) as MBikes,
SUM(case when detail.OrderQty > 1 and subcat.ProductSubcategoryID = 2 then 1 else 0 end) as Chains,
SUM(case when detail.OrderQty > 1 and subcat.ProductSubcategoryID = 3 then 1 else 0 end) as Gloves
from
sales.salesorderdetail detail
inner join sales.SalesOrderHeader header on header.SalesOrderID = detail.SalesOrderID
inner join sales.SalesPerson sp on sp.BusinessEntityID = header.SalesPersonID
inner join sales.SalesTerritory terr on terr.TerritoryID = sp.TerritoryID
inner join sales.SpecialOfferProduct sop on sop.ProductID = detail.ProductID
inner join Production.Product on Product.ProductID = sop.ProductID
inner join Production.ProductSubcategory subcat on subcat.ProductSubcategoryID = Product.ProductSubcategoryID
group by terr.Name, sp.Name