如何在MySQL中使用子查询显示两个表的混合结果?

如何在MySQL中使用子查询显示两个表的混合结果?,mysql,sql,Mysql,Sql,也许问题的标题不完全正确。我会尽力更好地解释我的问题。 我有两张表,结构如下: 表01: ID | GENUS | SPECIES | INDIVIDUUM 1 | A | a | alfa 2 | B | b | beta 3 | C | c | gama 4 | D | d | delta 5 | E | e | epsilon 表02: ID1 | ID2 | INDEX 1

也许问题的标题不完全正确。我会尽力更好地解释我的问题。 我有两张表,结构如下:

表01:

ID | GENUS | SPECIES | INDIVIDUUM
 1 |     A |       a | alfa
 2 |     B |       b | beta
 3 |     C |       c | gama
 4 |     D |       d | delta
 5 |     E |       e | epsilon
表02:

ID1 | ID2 | INDEX
  1 |   2 |   21%
  1 |   3 |   17%
  1 |   4 |   32%
  1 |   5 |   43%
  2 |   1 |   21%
  2 |   3 |   19%
  2 |   4 |   94%
  2 |   5 |   91%
  .     .       .
  .     .       .
  .     .       .
  5 |   1 |   43%
  5 |   2 |   91%
  5 |   3 |   83%
  5 |   4 |   76%
然后我选择以下选项:

SELECT id FROM table01 WHERE individuum in (alfa,epsilon);
并得到以下结果:

| ID |
|  1 |
|  5 |
这样我就可以做另一个选择:

SELECT * FROM table02 WHERE ID1 in (1,5);
你可能已经知道,这次我收到了以下结果:

| ID |
|  1 |
|  5 |
结果52:

ID1 | ID2 | INDEX
  1 |   2 |   21%
  1 |   3 |   17%
  1 |   4 |   32%
  1 |   5 |   43%
  5 |   1 |   43%
  5 |   2 |   91%
  5 |   3 |   83%
  5 |   4 |   76%
现在,我想基于此结果创建一个新表,该表将为我提供以下字段(和值):

它以“分析”的方式呈现第一个表中的数据,这些数据以某种方式相互关联,就像RESULT02以“合成”的方式向我们展示的一样

我想是这样的,但我不确定它是否正确

SELECT
b.id1,
(SELECT a.individuum FROM table01 a WHERE id = a.id1) individuum,
(SELECT a.species FROM table01 a WHERE id = a.id1) species,
(SELECT a.genus FROM table01 a WHERE id = a.id1) genus,
b.id2,
(SELECT a.individuum FROM table01 a WHERE id = a.id2) individuum2,
(SELECT a.species FROM table01 a WHERE id = a.id2) species2,
(SELECT a.genus FROM table01 a WHERE id = a.id2) genus2,
b.index
FROM
    table02 b
WHERE
    individuum in (alfa , epsilon);
ORDER BY index DESC";
如果您能帮助创建更好、更快、更高效的查询,我将不胜感激。

请尝试这种方式。只需重命名(使用别名)字段

select
   t2.id, spec1.individuum, spec1.species, spec1.genus,
   t2.id2, spec2.individuum, spec2.species, spec2.genus
   t2.INDEX
from
    table2 t2 
    table1 spec1,
    table2 spec2
where t2.id1 = spec1.id
  and t2.id2 = spec2.id

然后,您可以在('alfa','epsilon')或(1,5)

中添加任何您想要的条件,比如您自己的
spec1.individium中的

SELECT
  table01.id,
  table01.individuum,
  table01.species,
  table01.genus,
  -- -----------------
  table02.id as id2,
  -- -----------------
  t01.individuum as individuum2
  t01.species as species2, 
  t01.genus as genus2,
  -- -----------------
  table02.index
from
  table01
inner join
  table02 on table01.ID = table02.ID1
left join
  table01 as t01 on t01.ID = table02.ID2
where
    table01.individuum in (alfa , epsilon)
order by
  index DESC;

我想你需要这样的东西:

SELECT
  t1a.*
  t1b.*
  t2.index
FROM table02 t2
JOIN table01 t1a ON t1a.id=t2.id1
JOIN table01 t1b ON t1b.id=t2.id1
WHERE t2.id1 IN (SELECT id FROM table01 WHERE individuum in (alfa,epsilon))
ORDER BY t2.index DESC
SELECT
  t1a.id,
  t1a.individuum,
  t1a.species,
  t1a.genus,
  t1b.id AS id2,
  t1b.individuum AS individuum2,
  t1b.species AS species2,
  t1b.genus AS genus2,
  t2.index
FROM table02 t2
JOIN table01 t1a ON t1a.id=t2.id1
JOIN table01 t1b ON t1b.id=t2.id1
WHERE t2.id1 IN (SELECT id FROM table01 WHERE individuum in (alfa,epsilon))
ORDER BY t2.index DESC
如果需要显式解决列名冲突,则可以这样做:

SELECT
  t1a.*
  t1b.*
  t2.index
FROM table02 t2
JOIN table01 t1a ON t1a.id=t2.id1
JOIN table01 t1b ON t1b.id=t2.id1
WHERE t2.id1 IN (SELECT id FROM table01 WHERE individuum in (alfa,epsilon))
ORDER BY t2.index DESC
SELECT
  t1a.id,
  t1a.individuum,
  t1a.species,
  t1a.genus,
  t1b.id AS id2,
  t1b.individuum AS individuum2,
  t1b.species AS species2,
  t1b.genus AS genus2,
  t2.index
FROM table02 t2
JOIN table01 t1a ON t1a.id=t2.id1
JOIN table01 t1b ON t1b.id=t2.id1
WHERE t2.id1 IN (SELECT id FROM table01 WHERE individuum in (alfa,epsilon))
ORDER BY t2.index DESC

太多的子选择。。。我认为某些联接会更有效。我求求你,表之间的简单联接会更好。你实际上不存储“%”,是吗?Frazz和Jorge Campos,我正在测试你的解决方案,但它不起作用。我会把问题写在你各自的答案下面。谢谢@草莓,别担心,我不存储“%”;-)