sql-order-by

mySQL CAST and ASC / DESC

二次信任 提交于 2019-12-25 04:19:54
问题 I have a mysql_query SELECT * FROM table ORDER BY CAST(row AS DECIMAL) and want to change the sort direction. I tried ASC and DESC but both return exactly the same sort direction. How do I resort it? Thanks, greets Julian 回答1: That should work. Have you tried to display that, what your CAST(row AS DECIMAL) produces? Paste example of data that you trying to sort. Maybe you have nulls there or numbers that cant fit into decimal type. What version of mysql you are using? Maybe reason is this

Aliases not working in ORDER BY clause

▼魔方 西西 提交于 2019-12-25 03:28:17
问题 This will work fine in MySQL 5: SELECT INSTR(foo, 'Bar') as foobar FROM Table ORDER BY CASE WHEN foobar = 1 THEN foo END DESC; Bu in MySQL 4, I get the error: ERROR 1054 (42S22): Unknown column 'foobar' in 'order clause' However, if I change the clause to this, it will work on both versions: SELECT INSTR(foo, 'Bar') as foobar FROM Table ORDER BY CASE WHEN INSTR(foo, 'Bar') = 1 THEN foo END DESC; To ensure compatibility, do I have to always use the second way? 回答1: because foobar is an alias.

Order column by certain criteria in mysql

ぃ、小莉子 提交于 2019-12-25 02:19:25
问题 I need to be able to order my output data in a proper way using MySQL . I'm using ORDER BY so far and everything was working correctly until now. Let's say I have something like this: Table VEHICLES : ID | Name | MainGroup | SubGroup 1 | A | Vehicle | Truck 2 | B | Vehicle | Car 3 | C | Vehicle | Car 4 | D | Vehicle | Truck 5 | E | Vehicle | Truck 6 | F | Vehicle | Motorbike I was using this: SELECT * FROM Vehicles WHERE MainGroup=Vehicle ORDER BY Subgroup; When I get selections they are not

Order by in Thymeleaf

喜你入骨 提交于 2019-12-25 01:32:12
问题 I need show this foreach by order Asc , how can I do it? <tr th:each="ment : ${mentor}" th:if="${ment.jobId == job.id}"> <td th:text="${ment.id}"></td> <td th:text="${ment.name}"></td> <td th:text="${ment.qtyMentee}"></td> <td th:text="${ment.jobId}"></td> </tr> 回答1: This could be achieved with sort utility methods for lists, described here. /* * Sort a copy of the given list. The members of the list must implement * comparable or you must define a comparator. */ ${#lists.sort(list)} ${#lists

Order by date asp.net MVC 5

ぐ巨炮叔叔 提交于 2019-12-24 17:38:25
问题 I have a application that creates News Entries and displays 10 news. It should display the 10 "newest" news. As it is now it display the 10 oldest news. How can I change that.. Do I change the controller so that the data is sorted by date? Or can I do it in the view? Controller: public ActionResult Index() { return View(db.News.ToList()); } // GET: /Newss/Create public ActionResult Create() { return View(); } // POST: /Newss/Create // To protect from overposting attacks, please enable the

SQL Ordering Data from row “n+1” to “n-1”

混江龙づ霸主 提交于 2019-12-24 17:22:24
问题 I have a simple question. In MySQL, consider a row "n", how can we order rows by id (for example), but start from the row "n+1" and end to the row "n-1" ? Thanks ! EDIT : I ommit to precise that I seek the query in MySQL. From an answer below, here an example : ID --- 1 2 3 4 <--N 5 6 I want Desired Results ordered as follows 5 <--N + 1 6 1 2 3 <--N - 1 回答1: So you mean. For a table ID --- 1 2 3 4 <--N 5 6 You want Desired Results ordered as follows? 5 <--N + 1 6 1 2 3 <--N - 1 If so SELECT

Select all items that have matching tags

我只是一个虾纸丫 提交于 2019-12-24 17:16:04
问题 Suppose I have the following Django models: class Article(models.Model): title = models.CharField(max_length=200) blog = models.CharField(max_length=255) rating = models.IntegerField(default=0) class ArticleTag(models.Model): article = models.ForeignKey(Article) tag = models.CharField(max_length=200) Add some data: ArticleID Rating Blog ----------------------------------------- article1 -> 1 3 CNN article2 -> 2 2 BBC article3 -> 3 5 BBC article4 -> 4 9 NTV ArticleID tag ------------------- 1

How to avoid a filesort on a mysql composite index for a prefix query with an order by statement?

大憨熊 提交于 2019-12-24 16:44:21
问题 I have a 1 GB mysql table with three colums (german bigrams): create table sortedindex (source varchar(60),target varchar(60),score float) engine=myisam character set utf8 collate utf8_bin; I also created a composite index: create index sortedstd_ix on sortedindex (source(60), target(60), score); additionally I compressed the table and made it read only and sorted the index using: myisamchk --keys-used=0 -rq sortedindex myisampack sortedindex myisamchk -rq sortedindex --sort_buffer=3G --sort

PHP sort array()

北城余情 提交于 2019-12-24 12:16:57
问题 I have an array which looks like: array( 'parent1' => array( name = 'somename', childs = array('child1', 'child2', 'child3', 'child4') ), 'parent2' => array( name = 'somename', childs = array('child1') ), 'parent3' => array( name = 'somename', childs = array('child1', 'child2', 'child3', 'child4', 'child5') ) 'parent4' => array( name = 'somename', childs = array('child1', 'child2', 'child3') ), 'parent5' => array( name = 'somename', childs = array('child1', 'child2', 'child3', 'child4',

SQL Order By not working properly

懵懂的女人 提交于 2019-12-24 11:59:12
问题 I have a table like this CREATE TABLE [dbo].[tbl_LandRigs]( [ID] [int] IDENTITY(700000,1) NOT NULL, [Company] [nvarchar](500) NULL, [Rig] [nvarchar](500) NULL, [RigType] [nvarchar](200) NULL, [DrawWorks] [nvarchar](500) NULL, [TopDrive] [nvarchar](200) NULL, [RotaryTable] [nvarchar](500) NULL, [MudPump] [nvarchar](500) NULL, [MaxDD] [nvarchar](50) NULL, [Operator] [nvarchar](500) NULL, [Country] [nvarchar](200) NULL, [Location] [nvarchar](500) NULL, [OPStatus] [nvarchar](200) NULL,