sql-order-by

Order a MySQL table by two columns

微笑、不失礼 提交于 2019-12-17 00:47:03
问题 How do I sort a MySQL table by two columns? What I want are articles sorted by highest ratings first, then most recent date. As an example, this would be a sample output (left # is the rating, then the article title, then the article date) 50 | This article rocks | Feb 4, 2009 35 | This article is pretty good | Feb 1, 2009 5 | This Article isn't so hot | Jan 25, 2009 The relevant SQL I'm using is: ORDER BY article_rating, article_time DESC I can sort by one or the other, but not both. 回答1:

What is the “Default order by” for a mysql Innodb query that omits the Order by clause?

北慕城南 提交于 2019-12-16 18:02:23
问题 So i understand and found posts that indicates that it is not recommended to omit the order by clause in a SQL query when you are retrieving data from the DBMS. Resources & Post consulted (will be updated): SQL Server UNION - What is the default ORDER BY Behaviour When no 'Order by' is specified, what order does a query choose for your record set? https://dba.stackexchange.com/questions/6051/what-is-the-default-order-of-records-for-a-select-statement-in-mysql Questions : See logic of the

Optimize an ORDER BY query

若如初见. 提交于 2019-12-14 04:19:31
问题 I'm at a loss. I have a table with about 100K rows. When querying this table results are usually snappy, about 2ms or so. But whenever I use an ORDER BY performance drops like a rock to about 120ms. I read the MySQL ORDER BY Optimization page but I can't say I understand everything. Especially the indexes are unclear to me. Ultimately I would like to run the following query: SELECT * FROM `affiliate_new_contracts` WHERE phone_brand IN ('Apple','Blackberry','HTC','LG','Motorola','Nokia',

Set Order By to ignore punctuation on a per-column basis

梦想与她 提交于 2019-12-14 04:16:40
问题 Is it possible to order the results of a PostgreSQL query by a title field that contains characters like [](),; etc but do so ignoring these punctuation characters and sorting only by the text characters? I've read articles on changing the database collation or locale but have not found any clear instructions on how to do this on an existing database an on a per-column basis. Is this even possible? 回答1: If you want to have this ordering in one particular query you can ORDER BY regexp_replace

MySql Ordering by specific field value first does not work

≡放荡痞女 提交于 2019-12-14 04:02:40
问题 Hello I have a news page, I want to show the user's city news at the top. For example these are the news ordered descending by time. +----------+-----------+-----------------------+ | CityCode | entrytime | newsheader | +----------+-----------+-----------------------+ | 11 | 3800 | great opening | | 10 | 3700 | flood alert | | 12 | 3600 | new mall | | 13 | 3500 | pollution at the city | | 13 | 3400 | new mayor | | 12 | 3300 | house fire | | 11 | 3200 | traffic accident | | 10 | 3000 |

How to order IGrouping without changing its type?

£可爱£侵袭症+ 提交于 2019-12-14 03:48:40
问题 I have an oject of the type IGrouping and would like to order the elements inside the group without changing the type of the object. In other words, I have var tmp = group.OrderBy(x => x); with group being of type IGrouping<int, someanonymousclass> and I want tmp to be of type IGrouping<int, someanonymousclass> as well. One workaround would be to change it to a Dictionary or a new anonymous class, but I want tmp to specificly be of type IGrouping<int, someanonymousclass> . In other words: I

php + mysql, order by name + starting at specific id

[亡魂溺海] 提交于 2019-12-14 03:45:15
问题 MySQL: id | name | ------------ 1 | Joe | 2 | Craig | 3 | Shawn | 4 | Ryan | 5 | Seth | PHP: $a = mysql_query("SELECT * FROM table_name ORDER BY name DESC"); what I want to do though is, I want to start at id: 3 , so it should output: 3,4,5,1,2 回答1: EDIT : Mark is correct. The earlier query was syntactically incorrect. Using dummy aliasés should work! Select id from ( SELECT id FROM table_name WHERE id >= 3 ORDER BY id ASC ) X UNION Select * from ( SELECT id FROM table_name WHERE id < 3 ORDER

MySQL query slow because of ORDER BY with Stored Functions

Deadly 提交于 2019-12-14 03:16:58
问题 My query goes from 15 seconds to 0.05 seconds when I remove the ORDER BY in the following query: simplified version: SELECT field1, fiedl2, field 3, FUNC1(1, 2) AS score1, FUNC2(1, 2) AS score2, FUNC3(1, 2) AS score3, FUNC4(1, 2) AS score4 FROM table WHERE field1 = 1 ORDER BY (score1 * 1 + score2 * 2 + score3 * 2 + score4 * 4) DESC; I have a couple of stored functions that calculate sub-scores. Except I have to order the result based on the total score. In the ORDER BY I use * 2 to add some

MYSQL Order By W/Count

[亡魂溺海] 提交于 2019-12-14 03:04:56
问题 My table has usernames and points of user, and I am working on a leader boards list. I would like to get the top ten players based on points. I have formulated this query as: SELECT users.username, users.points FROM users ORDER BY users.points DESC LIMIT 10 However, I would also like to get where the player stands in accordance to the number 1 player, without creating a new column for it. Is there a MYSQL query to get the ORDER BY DESC by points as well as COUNT from that ORDER to find the

Select top using SQL Server returns different output than select *

浪尽此生 提交于 2019-12-14 03:02:51
问题 I tried to get select top n data from a database based on alphabetical & numbering format. The output must order by alphabet first and number after that. When I try to get all data ( select * ), I get the correct output: select nocust, share from TB_STOCK where share = ’BBCA’ and concat(share, nocust) < ‘ZZZZZZZZ’ order by case when nocust like ‘[a-z]%’ then 0 else 1 end nocust | share -------+-------- a522 | BBCA b454 | BBCA k007 | BBCA p430 | BBCA q797 | BBCA s441 | BBCA s892 | BBCA u648 |