Here I have 3 tables. I want to pull all people that attended either Game A, or Game B or both. Here is a Venn diagram of what I'd like to get:
I'd obviously like to have a unique list of people (no duplicates). I started out using two inner joins, but that only gives me the inside circle where all 3 tables intersect (which makes sense).
So, what JOIN or combination should I use to pull this data?
Person
id
first
last
GameA
typeid
checkin_time
person_id
GameB
typeid
checkin_time
person_id
QUERY:
This is what I have tried, which only returns people that have attended BOTH Game A and Game B:
SELECT * FROM Person
INNER JOIN GameA ON Person.id = GameA.person_id
INNER JOIN GameB ON Person.id = GameB.person_id
I am able to get the results desired using this query as well, but I'm not sure if there is a better way to accomplish this:
SELECT * FROM Person, GameA, GameB
WHERE Person.id = GameA.person_id OR Person.id = GameB.person_id
GROUP BY Person.id
There are a few ways you could do this. One would be to make a UNION subquery returning only the person_id from both GameA and GameB then perform an INNER JOIN to limit Person to those only, or use an IN()` subquery:
SELECT
DISTINCT Person.*
FROM
Person
INNER JOIN (
SELECT person_id FROM GameA
UNION
SELECT person_id FROM GameB
) attendees ON Person.id = attendees.person_id
Alternatively, an IN() subquery:
SELECT
DISTINCT Person.*
FROM Person
WHERE id IN (
SELECT person_id FROM GameA
UNION
SELECT person_id FROM GameB
)
It might be faster than either of those though, to do the UNION on the outside. If you have FOREIGN KEY relationships defined where necessary, therefore enforcing indexing, you could do the INNER JOIN twice and then UNION the results of those:
SELECT p.*
FROM Person p INNER JOIN GameA ON p.id = GameA.person_id
UNION
SELECT p.*
FROM Person p INNER JOIN GameB ON p.id = GameB.person_id
Related
I have a primary table with two additional linked tables, each relationship has a mapping table, I've obfuscated this and simplified the scenario.
Person
id
name
Location
id
name
Person_Location
person_id
location_id
Nationality
id
name
Person_Nationality
person_id
nationality_id
I'm looking to query each person and return one row for each related table
Person Name, Location Name, Nationilty Name
Some of the related tables may not be populated and I'd like the row returned but an empty value representing where the data is missing.
I've got a query with INNER JOINS for Person, Location and Person_Location mapping table but when I add additional joins for the Nationality mapping table, this returns many more rows than exists? I'm happy with a single set of joins but repeating this for additional one is not bearing fruit. I can't seem to find any examples of how to do this although I may not be using the correct approach.
SELECT p.name, lo.name as location, na.name as nationality
FROM person p
INNER JOIN person_location pl ON p.id = pl.person_id
INNER JOIN location lo ON pl.location_id = lo.id
INNER JOIN person_nationality pn ON p.id = pn.person_id
INNER JOIN nationality na ON pm.nationality_id = na.id
You can use two scalar subqueries to get the result you want. For example:
select p.name,
(select l.name from location l join person_location pl
on pl.location_id = l.id and pl.person_id = p.id limit 1) as location,
(select n.name from nationality n join person_nationality pn
on pn.nationality_id = n.id and pn.person_id = p.id limit 1) as nationality
from person p
I'm trying to display the total number of rows in a full outer join table. I have the following code, but mysql says there is an error with duplicate columns. The 2 tables, actors and directors, have the same columns as they are supposed to provide similar information in their respective categories.
SELECT COUNT(*) FROM
(SELECT * FROM directors LEFT OUTER JOIN actors
ON directors.name = actors.name
UNION
SELECT * FROM directors RIGHT OUTER JOIN actors
ON directors.name = actors.name) AS table1;
What can be done to fix the code so it will run properly? FYI, the code from within the parenthesis runs fine. The problem only arises once I put in the SELECT COUNT(*) clause.
Becuase there are two name columns one is from directors table, another is from actors table, and you select * that will let DB engine confuse which name did you want to get.
if you only want to count total number you can try this.
SELECT COUNT(*) FROM
(
SELECT directors.name FROM directors LEFT OUTER JOIN actors
ON directors.name = actors.name
UNION
SELECT directors.name FROM directors RIGHT OUTER JOIN actors
ON directors.name = actors.name
) table1;
NOTE
I would suggest use select clear the columns and avoid using select *
It might be better to change the right join portion non-redundant, and just add separate
counts.
Generic version:
SELECT (SELECT COUNT(*) FROM A LEFT JOIN B ON A.x = B.x)
+ (SELECT COUNT(*) FROM B LEFT JOIN A ON B.x = A.x WHERE A.x IS NULL)
AS outerJoinSize
;
Note: I changed the RIGHT JOIN to a LEFT JOIN and swapped the tables around; in my experience, RIGHT JOIN just tends to make queries a little harder to read (especially when multiple joins are involved).
An completely different alternative...
SELECT
( SELECT SUM(dc1.c * IFNULL(ac1.c, 1)) AS jc
FROM (SELECT name, COUNT(*) AS c FROM directors GROUP BY name) AS dc1
LEFT JOIN (SELECT name, COUNT(*) AS c FROM actors GROUP BY name) AS ac1
ON dc1.name = ac1.name)
+ (SELECT SUM(IF(dc2.name IS NULL, ac2.c, 0)) AS jc
FROM (SELECT name, COUNT(*) AS c FROM actors GROUP BY name) AS ac2
LEFT JOIN (SELECT name, COUNT(*) AS c FROM directors GROUP BY name) AS dc2
ON ac2.name = dc2.name)
...this one figures out how many matches based on the joining field (3 instances of "Bob" in directors and 2 in actors means 6 join results for that name).
I'm not sure what you are getting at with the full join. But the best way to implement it in MySQL uses two left joins and a union:
select count(*)
from ((select name from directors) union -- on purpose
(select name from actors)
) da left join
directors d
on da.name = d.name left join
actors a
on da.name = a.name;
If I had to guess, though, you just want the number of distinct names between the two tables. If so:
select count(*)
from ((select name from directors) union -- on purpose
(select name from actors)
) da
So I have the below database structure
TABLES ------- Columns
person: id, name, salary, address
group: id, name
person_group: person_id, groud_id
So here is my query which is used to get all persons along with the groups they are associated with
SELECT p.id, p.name,
group_concat(g.name) as groups
FROM person_group pg, group g, person p
WHERE pg.group_id = g.id AND pg.novel_id = n.id
GROUP BY ng.person_id
So this query gives me data like
id name groups
2345 John Admin, SuperAdmin, RedHat
But the problem is: if that person doesn't belong to any group, the query doesn't return that person!
Any would be appreciated!
Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.
That is exactly your problem here. When you are writing more than one table in the FROM clause, you should be thinking "what type of JOIN do I need". If you had that thought, you would immediate realize that you need an outer join to do what you want:
SELECT p.id, p.name, group_concat(g.name) as groups
FROM person p LEFT JOIN
person_group pg
ON pg.person_id = p.id LEFT JOIN
group g
ON pg.group_id = g.id
GROUP BY p.id, p.name;
If you don't know what an outer join is, then that is all the more reason to use the proper, explicit syntax, so you can learn.
I am new to SQL,I have two tables RACER and SPONSOR,
RACER TABLE has these columns
RACER_NAME,
SPONSOR_ID
RACER_ID- Primary KEY
SPONSOR table has these columns
SPONSOR_ID,
SPONSOR_NAME
now I want to find the SPONSOR name and no.of racer associated with that SPONSOR.
Here is what I tried:
select s.sponsor_name , (select count(*) from racer r) where INNER JOIN s.sponsor_id = r.sponsor_id
You need to understand, how JOIN works and its syntax.
select s.sponsor_name, count(*) as total_racer
from
racer r inner join sponsor s
on r.sponsor_id=s.sponsor_id
group by r.sponsor_id
You need to specify both tables in FROM clause, which you were missing.
You could use a join ( left join if not al the sponsor have racer ) and get the result without subselect using group by and count
select s.sponsor_name , count(*)
from SPONSOR s r
left JOIN racer r s.sponsor_id = r.sponsor_id
GROUP BY s.sponsor_name
Your version, with a subquery is reasonable, particularly because in MySQL it can have better performance than the corresponding GROUP BY query. However, it needs to be a correlated subquery. That looks like:
select s.*,
(select count(*)
from racer r
where s.sponsor_id = r.sponsor_id
) as cnt
from sponsor s;
In other words, choose either the JOIN method or the subquery method, but not both for the same value.
Looking at similar questions, I actually want the exact opposite of this:
SQL query for getting data in two fields from one column
I have a table meetings with paired users:
A_user_id | B_user_id
1 2
3 4
There is a user table as well.
Is there a simple mysql query that lists all the user_ids into one long list?
query result
1
2
3
4
I was thinking something like this but it doesn't work:
select *
from user
where user.id in (
(select A_user_id from meeting)
or
(select B_user_id from meeting)
)
Thanks!
UPDATE (UNION solved this, but let's make this a bit more challenging):
I want to get a list of usernames and location names (both are reference tables) so I need to join this union query to them. Here's what I tried:
select u1.fname, l1.name
from meeting m1
join user u1 on m1.A_user_id=u1.id
join locations l1 on m1.location_id=l1.id
union
select u2.fname, l2.name
from meeting m2
join user u2 on m2.A_user_id=u2.id
join locations l2 on m2.location_id=l2.id
order by location_id asc
I'm getting two errors:
1- Not sure what kind of joins I need on these. (without the last 'order by' line) I'm getting a list of only 2 (there should be 4, as there are 2 pairs of people meeting). It seems to be pulling only the first item from each part of the union. I believe this relates to the type of join I'm doing for each, but not sure. So, users are distinct (there is only 1 user in the meeting table and it matches only 1 user in the user table), but locations are not (2 users are meeting at 1 location, and I think when I join on locations it is messing things up).
2- How do I use the "order by" at the end to order by the resulting list of "location_id"s, since now I have two named tables to deal with.
Thanks!
UPDATE 2:
Ok I put the two selects into parenthesis and UNIONed them and now I can order by the location_id... but I still have no idea how to join on the location table. Mysql doesn't like what I tried
(select u1.fname, m1.location_id
from meeting m1
join user u1 on m1.A_user_id=u1.id)
union
(select u2.fname, m2.location_id
from meeting m2
join user u2 on m2.B_user_id=u2.id)
#join locations l on l.id = location_id // this line messes things up *
order by location_id asc
Doesn't there need to be an all encompassing select around this whole thing?
How do I join the locations.id field on the "location_id" field that gets kicked off of the union query? Since the "location_id" field is technically in two different tables?
THe join above throws an error.
UPDATE 3: SOLVED
Here's my final query:
select tb1.fname, l.name
from (
(select u1.fname, m1.location_id
from meeting m1
join user u1 on m1.A_user_id=u1.id)
union
(select u2.fname, m2.location_id
from meeting m2
join user u2 on m2.B_user_id=u2.id)
) tb1
join locations l on l.id = tb1.location_id
order by location_id asc
select A_user_id as id from meetings
union
select B_user_id as id from meetings
in your example code, you could use an 'or', but the 'or' has to join two 'in' statements, if you get what I mean.
select *
from user
where
(
(user.id in (select A_user_id from meeting))
or
(user.id in ((select B_user_id from meeting))
)
And to answer you second update, you want something like
select locations.* from
(
(select A_user_id as id from meeting)
union
(select B_user_id as id from meeting)
) as UIDS
join
locations on locations.id = UIDS.id
select A_user_id as user_id from meetings
union all
select B_user_id as user_idfrom meetings
order by user_id
Notes:
UNION ALL keeps duplicates, UNION doesn't
Any ORDER BY goes at the end of the UNION