简体   繁体   English

将两个SELECT查询合并为一个

[英]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. 我对UNIONS不太熟悉。 I have two SELECT queries which i would like to merge in one. 我有两个SELECT查询,我想合并成一个。 So i want to select the name based on userId from table Teams and their gameAcc from table connections based on teamsId. 所以我想从基于表Teams的userId选择名称,并从基于teamId的表连接选择它们的gameAcc。

在此处输入图片说明

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. 预期:单个查询将输出1行,其中包含来自teams表的用户名和来自connections表的gameAcc。 Example data: 示例数据:

在此处输入图片说明

And i want it to output: 我希望它输出:

|John| | JohnKiller |

Thanks for your help! 谢谢你的帮助!

You need to join the 3 tables and not UNION: 您需要加入3个表而不是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. 我使用INNER联接就像您的代码一样。
Depending on the case maybe you need LEFT joins if there is no WHERE clause. 根据情况,如果没有WHERE子句,则可能需要LEFT连接。
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;

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

EDIT: From the comments above, you seem to want the name of the owner, and the gameAcc. 编辑:从上面的评论,您似乎想要所有者的名称和gameAcc。 In my example below, I changed teams.usersId to "ownerId", to avoid confusion. 在下面的示例中,我将team.usersId更改为“ ownerId”,以避免造成混淆。 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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM