Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从mysql中的嵌套查询获取结果后在desc中排序_Mysql_Sql - Fatal编程技术网

从mysql中的嵌套查询获取结果后在desc中排序

从mysql中的嵌套查询获取结果后在desc中排序,mysql,sql,Mysql,Sql,我有两张桌子。我按desc格式的顺序得到结果。 现在我想以相同的顺序格式显示信息。但我无法做到这一点 select * from table1 where field in (select * from table2 where StartDate > '2011-11-01' AND StartDate < '2011-11-30'

我有两张桌子。我按desc格式的顺序得到结果。 现在我想以相同的顺序格式显示信息。但我无法做到这一点

select *
  from table1  
 where field in (select * 
                   from table2 
                  where StartDate > '2011-11-01' 
                    AND StartDate < '2011-11-30'
               group by field1 
               order by count(field1) desc );
内部查询按降序排列,但与外部查询一起使用时,顺序会丢失。

in子句不保留顺序,我很惊讶MySQL竟然允许这样做:

一种解决方案是计算子查询中的计数,并将其用于排序:

select  *
from    table1 t1
join    (
        select  field1
        ,       count(field1) as Field1Count
        from    table2 
        where   StartDate > '2011-11-01' 
                and StartDate < '2011-11-30'
        group by field1 
        ) t2
on      t1.field1 = t2.field1
order by
        t2.Field1Count desc

该查询在将table1.field与table2中的所有列进行比较时应返回一个错误…您确定这是您的实际工作查询吗?select*from中的where字段看起来很奇怪。是。但我找到了答案。谢谢