MySQL按另一个表的值排序

时间:2014-09-25 12:02:45

标签: mysql sql sql-order-by

我有2个MySQL表格(来自CMS" contenido",如果你知道的话):

art_lang


idartlang  published (Timestamps)
1          2012-09-20 13:27:47
2          2012-09-21 13:27:47
3          2012-09-12 13:27:47
4          2012-19-22 13:27:47
5          2012-09-25 13:27:47

内容

idartlang  content_type_id content_field_id value
1          2               5                foo
1          9               20               K
2          2               5                bar
3          2               5                foobar
3          9               20               C
4          9               20               Z

现在我想从Table" art_lang"中选择文章ID,但是按照"内容"的值排序。其中content_type_id = 9 AND content_field_id = 2。 如果此Article_ID没有条目,请通过art_lang.published对其他值进行排序......

这样我的例子就会产生以下结果:

预期结果

art_lang.idartlang  contents.value art_lang.published
3                   C              2012-09-12 13:27:47
1                   K              2012-09-20 13:27:47
4                   Z              2012-09-22 13:27:47
5                                  2012-09-25 13:27:47
2                                  2012-09-21 13:27:47

给定的查询如下所示:

查询

SELECT DISTINCT a.idartlang 
FROM art_lang AS a, art AS b, cat_art AS c, content AS cont 
WHERE c.idcat IN ('1374') 
AND b.idart = c.idart 
AND a.idartlang NOT IN ('2233') 
AND b.idclient = '4'
AND a.idlang = '5' 
AND a.idart = b.idart 
AND a.online = 1 
ORDER BY a.published DESC

所以你看,已经存在一些依赖...

希望你明白我想要的东西:)

1 个答案:

答案 0 :(得分:0)

这是单程......

SELECT a.Article_ID
     , c.value 
     , a.published
  FROM art_lang a
  LEFT 
  JOIN contents c
    ON c.article_id = a.article_id
   AND c.content_type_id = 9 
   AND c.content_field_id = 2
 ORDER 
    BY c.value IS NULL, c.value