SQL:一个查询中有两个select语句

时间:2015-08-13 03:26:19

标签: sql select

我想在一个查询中从两个SQL表中选择信息,但信息不相关,因此不存在潜在关节。

示例可以是以下设置。

tblMadrid

   id | name    | games | goals
    1 | ronaldo | 100   | 100
    2 | benzema | 50    | 25
    3 | bale    | 75    | 50
    4 | kroos   | 80    | 10

tblBarcelona

   id | name    | games | goals
    1 | neymar  | 60    | 25
    2 | messi   | 150   | 200
    3 | suarez  | 80    | 80
    4 | iniesta | 40    | 5

我想要一个查询,它给我以下内容:

name    | games | goals
messi   | 150   | 200
ronaldo | 100   | 100

我尝试遵循以下逻辑:Multiple select statements in Single query但以下代码无效:

USE Liga_BBVA

SELECT (SELECT name,
               games,
               goals
        FROM   tblMadrid
        WHERE  name = 'ronaldo') AS table_a,
       (SELECT name,
               games,
               goals
        FROM   tblBarcelona
        WHERE  name = 'messi')   AS table_b
ORDER  BY goals 

对此有何建议?谢谢 信息:足球的东西只是一个简化的例子。实际上,不可能将两个表放在一个并且拥有一个新的"团队"柱。这两个表有完全不同的结构,但我需要一些与这个例子的特征相符的东西。

8 个答案:

答案 0 :(得分:31)

您可以这样做:

 (select
    name, games, goals
    from tblMadrid where name = 'ronaldo')
 union
 (select
    name, games, goals
    from tblBarcelona where name = 'messi')
order by goals;

例如,请参阅:https://dev.mysql.com/doc/refman/5.0/en/union.html

答案 1 :(得分:6)

如果您希望将记录分开并且不进行联合。
尝试下面的查询

SELECT (SELECT name,
               games,
               goals
        FROM   tblMadrid
        WHERE  name = 'ronaldo') AS table_a,
       (SELECT name,
               games,
               goals
        FROM   tblBarcelona
        WHERE  name = 'messi')   AS table_b
FROM DUAL

答案 2 :(得分:4)

UNION声明是你的朋友:

SELECT   a.playername, a.games, a.goals
FROM     tblMadrid as a
WHERE    a.playername = "ronaldo"
UNION
SELECT   b.playername, b.games, b.goals
FROM     tblBarcelona as b
WHERE    b.playername = "messi"
ORDER BY goals;

答案 3 :(得分:2)

只要列匹配,您就可以合并查询。

SELECT name,
       games,
       goals
FROM   tblMadrid
WHERE  id = 1
UNION ALL
SELECT name,
       games,
       goals
FROM   tblBarcelona
WHERE  id = 2 

答案 4 :(得分:1)

select name, games, goals
from tblMadrid where name = 'ronaldo'
union
select name, games, goals
from tblBarcelona where name = 'messi'
ORDER  BY goals 

答案 5 :(得分:0)

您可以组合两个表中的数据,按目标排名最高,然后选择前两个,如下所示:

<强>的MySQL

select *
from (
  select * from tblMadrid
  union all
  select * from tblBarcelona
) alldata
order by goals desc
limit 0,2;

SQL Server

select top 2 *
from (
  select * from tblMadrid
  union all
  select * from tblBarcelona
) alldata
order by goals desc;

如果你只想要梅西和罗纳尔多

select * from tblBarcelona where name = 'messi'
union all
select * from tblMadrid where name = 'ronaldo'

为了确保messi位于结果的顶端,您可以执行以下操作:

select * from (
  select * from tblBarcelona where name = 'messi'
  union all
  select * from tblMadrid where name = 'ronaldo'
) stars
order by name;

答案 6 :(得分:0)

在这种情况下,使用union会有所帮助。

您还可以在始终返回true且与这些表中的数据无关的条件下使用join。参见下文

从tblMadrid选择tmd .name,tbc.goals tmd join tblBarcelona tbc on 1 = 1;

答案 7 :(得分:-1)

在这种情况下你可以使用UNION

select id, name, games, goals from tblMadrid
union
select id, name, games, goals from tblBarcelona

你必须维护所选列的顺序,即两个SQL中的id,名称,游戏,目标