orderby方法在laravel中使用2个案例

时间:2018-01-01 09:55:19

标签: php sql laravel sql-order-by case

我可以在laravel中使用orderby方法 像这样

return $this->hasMany('Link')->orderBy('quality', 'desc');

没关系,我想要像这样使用

CASE `lang`
         WHEN 'a' THEN 1
         WHEN 'as' THEN 2
         WHEN 'asd' THEN 3
         END
,CASE `quality`
         WHEN 'd' THEN 1
         WHEN 'ds' THEN 2
         WHEN 'dsa' THEN 3
         WHEN 'dsaa' THEN 4
         WHEN 'dsaaa' THEN 6
         WHEN 'dsaaaa' THEN 7
         WHEN 'dsaaaaa' THEN 8
         WHEN 'dsaaaaaaaaa' THEN 9
         WHEN 'dsaaaa' THEN 10
         END,views DESC

我怎么能在laravel中写这个?

2 个答案:

答案 0 :(得分:2)

试试这个:

 df = spark.createDataFrame([('R101','GTR001'),('R201','RTY987'),('R301','KIT158'),('R201','PLI564'),('R101','MJU098'),('R301','OUY579')],['id','code'])
 df.show()
   +----+------+
   |  id|  code|
   +----+------+
   |R101|GTR001|
   |R201|RTY987|
   |R301|KIT158|
   |R201|PLI564|
   |R101|MJU098|
   |R301|OUY579|
   +----+------+

 from pyspark.sql import functions as F
 from pyspark.sql import Window

 df = df.withColumn('rank',F.dense_rank().over(Window.orderBy("id","code")))
 df.withColumn('combcol',F.concat(F.lit('col_'),df['rank'])).groupby('id').pivot('combcol').agg(F.first('code')).show()

    +----+------+------+------+------+------+------+
   |  id| col_1| col_2| col_3| col_4| col_5| col_6|
   +----+------+------+------+------+------+------+
   |R101|GTR001|MJU098|  null|  null|  null|  null|
   |R201|  null|  null|PLI564|RTY987|  null|  null|
   |R301|  null|  null|  null|  null|KIT158|OUY579|
   +----+------+------+------+------+------+------+ 

答案 1 :(得分:0)

这些数字背后有算法吗? ORDER BY LENGTH(`lang`), LENGTH(`quality`), views DESC可以做这个工作吗?