popcorn
popcorn

Reputation: 408

Union two SELECT queries in one

as i can't brain this out I'm asking for your help. I'm not so familiar with UNIONS. I have two SELECT queries which i would like to merge in one. So i want to select the name based on userId from table Teams and their gameAcc from table connections based on teamsId.

enter image description here

I've made two separate queries:

SELECT users.name 
FROM users 
JOIN teams 
  ON users.id = teams.usersId 
WHERE teams.Id = 1

SELECT connections.gameAcc 
FROM connections 
JOIN users 
  ON connections.usersId = users.id 
JOIN teams 
  ON connections.teamsId = teams.id 
WHERE teams.id = 1

Expected: A single query that will output 1 row with users name from teams table and their gameAcc from connections table. Example data:

enter image description here

And i want it to output:

|John| | JohnKiller |

Thanks for your help!

Upvotes: 0

Views: 306

Answers (3)

ESR
ESR

Reputation: 46

EDIT: From the comments above, you seem to want the name of the owner, and the gameAcc. In my example below, I changed teams.usersId to "ownerId", to avoid confusion. I'm assuming that the connections table is joined to the users table, and also to the teams table.

https://www.tutorialspoint.com/execute_sql_online.php

BEGIN TRANSACTION;

/* Create tables for example: */
CREATE TABLE USERS(Id integer PRIMARY KEY, Name text);
CREATE TABLE TEAMS(Id integer PRIMARY KEY, Name text, ownerId integer);
CREATE TABLE CONNECTIONS(Id integer PRIMARY KEY, usersId integer, teamsId integer, gameAcc text);

/* Create new records in these tables */
INSERT INTO USERS VALUES(1,'Jim');
INSERT INTO USERS VALUES(2,'Paul');
INSERT INTO USERS VALUES(3,'Tony');
INSERT INTO USERS VALUES(4,'Bill');
INSERT INTO USERS VALUES(5,'Art');
INSERT INTO TEAMS VALUES(11,'Tigers', 1); -- owner = user.id = 1
INSERT INTO TEAMS VALUES(22,'Raptors', 2); -- owner = user.id = 2
INSERT INTO CONNECTIONS VALUES(111, 1, 11, 'gameAcc1');
INSERT INTO CONNECTIONS VALUES(222, 2, 22, 'gameAcc2');
INSERT INTO CONNECTIONS VALUES(333, 3, 22, 'gameAcc2');
INSERT INTO CONNECTIONS VALUES(444, 4, 22, 'gameAcc2');
INSERT INTO CONNECTIONS VALUES(555, 5, 11, 'gameAcc2');
COMMIT;

/* Display desired records from the tables */
SELECT users.name, connections.gameAcc 
FROM users
JOIN connections ON connections.usersId = users.id
JOIN teams ON connections.teamsId = teams.id
WHERE teams.id = 11 AND teams.ownerId = users.id

Upvotes: 0

forpas
forpas

Reputation: 164194

You need to join the 3 tables and not UNION:

SELECT users.name, connections.gameAcc 
FROM users 
INNER JOIN teams ON users.id = teams.usersId 
INNER JOIN connections ON connections.teamsId = teams.id
WHERE teams.Id = 1

I used INNER joins just like your code.
Depending on the case maybe you need LEFT joins if there is no WHERE clause.
Edit, after the questions updates.

SELECT u.name, c.gameAcc 
FROM connections c
JOIN users u ON c.usersId = u.id 
JOIN teams t1 ON c.teamsId = t1.id 
JOIN teams t2 ON u.id = t2.usersId 
WHERE t2.id = 1;

Upvotes: 3

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48207

Split it on steps.

First you want to know the owner

  SELECT t.userid
  FROM teams t
  WHERE t.teamid = 1

Now you want the owner name

  SELECT t.userid, u.name
  FROM teams t
  JOIN users u 
    ON t.userid = u.id
  WHERE t.teamid = 1

Now you want the game acc for the owner

  SELECT t.userid, u.name, c.gameAcc
  FROM teams t
  JOIN users u 
    ON t.userid = u.id
  JOIN connection c
    ON t.userid = c.userid
  WHERE t.teamid = 1

Upvotes: 0

Related Questions