如何在mysql中对数据类型tinyint进行排序

时间:2015-02-13 09:12:37

标签: mysql

我需要什么

  • 我需要在mysql中对整数进行排序。

取消订阅tinyint(6)

1,1,4,5,1,1,......等

sql查询

 SELECT e0_.metadata AS metadata0, u1_.unsubscribe AS unsubscribe1, u1_.id AS id2, u1_.linkedin_id AS linkedin_id3, u1_.facebook_id AS facebook_id4, u1_.linkedin_profile AS linkedin_profile5, u1_.profile AS profile6, 
    CASE WHEN u1_.linkedin_id <> ''
    OR u1_.facebook_id <> ''
    THEN 1 
    ELSE 0 
    END AS sclr7, 
    CASE WHEN e0_.edition =23932
    THEN 'current'
    ELSE 'past'
    END AS sclr8
    FROM event_visitor e0_, user u1_
    WHERE e0_.user = u1_.id
    AND e0_.event =25162
    AND e0_.published =1
    AND e0_.showme =1
    GROUP BY e0_.user
    ORDER BY sclr7 DESC , e0_.id DESC , LENGTH( u1_.unsubscribe ) 
    LIMIT 30
  • 我试过的解决方案。

    1. 长度
    2. cast
    3. 按col * 1,col
    4. 排序

但没有解决方案在tiny int数据类型中工作。

问题

  • 还有像-5这样的负数。

o / p应该是

all 1 should be at top .
最后经过谷歌搜索

  • 按照CAST命令(est_dily_pple as SIGNED INTEGER)ASC

  • 按顺序排序(u1_.unsubscribe AS SIGNED INTEGER)ASC,sclr7 DESC,e0_.id DESC

    • 但问题o / p -5,1,1,...等等。

解决方案我出现了:

  • ORDER BY SIGN(CAST(u1_.unsubscribe AS SIGNED INTEGER))ASC,ABS(CAST(u1_.unsubscribe AS SIGNED INTEGER)),sclr7 DESC,e0_.id DESC。

Refrence

  http://stackoverflow.com/questions/11282571/specific-order-by-in-mysql-statement

我需要实现abs函数学说

dql代码

     $from = 'Entities\EventVisitor visitor,Entities\User user';
   $qb = $this->em->createQueryBuilder();

  $qb->select("visitor.metadata,ABS( u1_.unsubscribe )as unsubscribe)
 ->add('from',$from)

 ->where('visitor.user=user.id')
 ->groupBy('visitor.user')
->orderBy('unsubscribe','asc')

error 500 internal error.

0 个答案:

没有答案