SQL多表子查询

时间:2016-10-04 20:57:49

标签: mysql sql

我不知道为什么,但我无法理解它。我有两张桌子:

mysql> select * from PETICIONES;                                                                                             +--------+------------+--------+-----------+-----------+
| ID_PET | FECHA_PET  | ID_ANA | DNI_PAC   | DNI_DOC   |
+--------+------------+--------+-----------+-----------+
|      1 | 2008-01-03 |      2 | 71515623A | 23456398F |
|      2 | 2008-05-10 |      2 | 33788976F | 55776898K |
|      3 | 2008-05-08 |      3 | 79876867X | 23456398F |
|      4 | 2008-05-11 |      4 | 44787345H | 55776898K |
|      5 | 2008-05-12 |      2 | 19887234W | 25349857H |
|      6 | 2008-05-05 |      4 | 22897576R | 55776898K |
|      7 | 2008-03-15 |      5 | 44787345H | 88647389P |
|      8 | 2008-03-19 |      1 | 71515623A | 23456398F |
|      9 | 2008-03-26 |      2 | 71515623A | 78988484B |
|     10 | 2008-03-15 |      2 | 19887234W | 88647389P |
|     11 | 2008-03-15 |      3 | 33788976F | 55776898K |
|     12 | 2008-03-26 |      2 | 44787345H | 23456398F |
+--------+------------+--------+-----------+-----------+

mysql> select * from TIPOS_ANALISIS;
+--------+-----------------+
| ID_ANA | DESC_ANA        |
+--------+-----------------+
|      1 | SANGRE BÁSICO   |
|      2 | SANGRE COMPLETO |
|      3 | ORINA BÁSICO    |
|      4 | ORINA COMPLETO  |
|      5 | HECES BÁSICO    |
|      6 | HECES COMPLETO  |
+--------+-----------------+

现在,我所要做的就是找到最有序的分析并显示它的详细信息。现在我可以找到比其他人订购的次数更多的分析但是加入这两个表以便我可以显示它的描述是我做得不太好的事情。我已经尝试了这个,但它只是拖了一个错误,我无法解决它(错误是:操作数应该包含1列):

Select * from TIPOS_ANALISIS
    -> where ID_ANA = ( SELECT ID_ANA, COUNT(ID_ANA) as AnaCount
    -> from PETICIONES
    -> group by ID_ANA
    -> order by AnaCount DESC
    -> limit 1);

有人可以帮忙解释一下吗?请?

如果我想显示多少计数怎么办?我该怎么做?

4 个答案:

答案 0 :(得分:1)

试试这个:

post_max_size

错误是子查询是标量子查询,这意味着它必须返回一列,最多一行。标量子查询用于代替SQL中的常量值。

我不是百分百肯定这会在MySQL中运行。 MySQL在某些子查询中对Select a.* from TIPOS_ANALISIS a where a.ID_ANA = (select p.ID_ANA from PETICIONES p group by p.ID_ANA order by AnaCount DESC limit 1 ); 很挑剔。

但是,您可以更多地使用limit子句:

from

答案 1 :(得分:1)

尝试使用

Select a.*
from TIPOS_ANALISIS a
where a.ID_ANA = (select b.ID_ANA
                  from PETICIONES b
                  group by b.ID_ANA
                  order by count(*) DESC
                  limit 1
                 );

答案 2 :(得分:1)

它已经告诉你应该只有一列。只需从内部查询中选择1列:

Select * from TIPOS_ANALISIS
where ID_ANA = (SELECT ID_ANA FROM ( SELECT ID_ANA, COUNT(ID_ANA) as AnaCount
from PETICIONES
group by ID_ANA
order by AnaCount DESC
limit 1) t1);

#Edit: To display counts as well

SELECT ID_ANA, COUNT(ID_ANA) AS count from TIPOS_ANALISIS
    WHERE ID_ANA = (SELECT ID_ANA FROM ( SELECT ID_ANA, COUNT(ID_ANA) as AnaCount
from PETICIONES
    group by ID_ANA
    order by AnaCount DESC limit 1) t1)
GROUP BY ID_ANA;

答案 3 :(得分:0)

您应该只从subselect

中选择ID_ANA
select * from TIPOS_ANALISIS 
where ID_ANA = ( SELECT ID_ANA 
                 from PETICIONES 
                 group by ID_ANA
                 order by COUNT(ID_ANA)
                 limit 1)