Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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/79.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按列表排序_Mysql_Sql_Select_Sql Order By - Fatal编程技术网

MYSQL按列表排序

MYSQL按列表排序,mysql,sql,select,sql-order-by,Mysql,Sql,Select,Sql Order By,我有一个表A,它有一个“ID”和“Message”,还有一个表B是有序的。我想根据表B中的订购方式订购表A 示例-表A: ID Message ------------- 1 ABC 2 DEF 3 HIJ 4 KLM 5 NOP 表B: ID --- 5 2 4 1 3 预期结果: ID Message ------------- 5 NOP 2 DEF 4 KLM 1 ABC 3 HIJ

我有一个表A,它有一个“ID”和“Message”,还有一个表B是有序的。我想根据表B中的订购方式订购表A

示例-表A:

ID    Message
-------------
1     ABC
2     DEF
3     HIJ
4     KLM
5     NOP
表B:

ID
---
5
2
4
1
3
预期结果:

ID    Message
-------------    
5     NOP
2     DEF
4     KLM
1     ABC
3     HIJ
我有以下疑问:

SELECT DISTINCT s.message, s.stream_id
FROM Stream s
ORDER BY ..... ;
我怎么做? 显然,在_集中使用FIND_并没有考虑表B的顺序

更新

表B的顺序如下:

SELECT stream_id 
FROM SearchCache 
GROUP BY stream_id 
ORDER BY COUNT(stream_id);
SELECT DISTINCT s.message, s.stream_id
FROM Stream s
ORDER BY s.stream_id *SOMETHING* (SELECT stream_id 
                                  FROM SearchCache 
                                  GROUP BY stream_id 
                                  ORDER BY COUNT(stream_id)) ASC;
因此,查询应如下所示:

SELECT stream_id 
FROM SearchCache 
GROUP BY stream_id 
ORDER BY COUNT(stream_id);
SELECT DISTINCT s.message, s.stream_id
FROM Stream s
ORDER BY s.stream_id *SOMETHING* (SELECT stream_id 
                                  FROM SearchCache 
                                  GROUP BY stream_id 
                                  ORDER BY COUNT(stream_id)) ASC;

由于您是按聚合
COUNT()
表B
进行排序,因此您可以在
排序中使用相同的值。只需将其添加到导出表B的
选择
列表中即可

SELECT
  DISTINCT 
  s.message, 
  s.stream_id
FROM 
  Stream s
  /* Join against a subquery that produces a count per stream_id */
  /* Using LEFT JOIN in case none exist for a given s.stream_id */
  LEFT JOIN (
    SELECT stream_id, COUNT(*) AS num_streams
    FROM SearchCache
    GROUP BY stream_id
   ) stream_counts ON s.stream_id = stream_counts.stream_id
/* Order by the COUNT() aggregate supplied by the joined subquery */
ORDER BY stream_counts.num_streams ASC
注意:如果您发布了
SearchCache
表的示例,我将使用SQLFIDLE示例进行更新…

尝试以下操作:

SELECT a.id, a.message 
FROM tablea a 
INNER JOIN (SELECT (@auto:=@auto+1) auto, id 
            FROM tableb, (SELECT @auto:=0) AS a) AS b ON a.id = b.id 
ORDER BY b.auto;

表B的
顺序如何?它似乎没有任何特定的顺序,并且您无法可靠地按照插入的顺序检索行……从表B中选择时,您是如何指定顺序的?如果您没有为表B指定ORDER BY,那么答案是,如果不指定ORDER BY,您就无法可靠地确定行的输出顺序。@Travesty3诸位伟人都一样,是吗?您在表B上有主键吗?那么
Table B
是插入SELECT
的结果,还是仅仅是一个查询?