sql-order-by

Mixing different categories results, ordered by score in MySQL

我们两清 提交于 2019-12-01 09:11:45
In my PHP application, I have a mysql table of articles which has the following columns: article_id articletext category_id score Each article has a score which is calculated based on how popular it is, and belongs to a specific category (there are around 10 categories available) My question is: how can I perform a query that returns the highest scored articles while alternating them by categories so that if possible, no same-category articles are returned consecutively. For example if the highest scored article has score: 100 the returning set would be something like: article_id articletext

How to work around ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc

 ̄綄美尐妖づ 提交于 2019-12-01 08:49:06
I want to lock one record in a table. The record is specified as "the next that has ID greater than..." CREATE TABLE test (id number); SELECT id FROM (SELECT id FROM test WHERE id > 10 ORDER BY id) WHERE ROWNUM = 1 FOR UPDATE; This seems intuitive and easy. But it is not. Any ideas? P.S. I do need the existing query to remain the same because it is a cursor and there are several places that use this cursor's %rowtype. I think you're going to need something like: SELECT id FROM test WHERE id = (SELECT MIN(id) FROM test WHERE id > 10) FOR UPDATE; 来源: https://stackoverflow.com/questions/3166615

SQLite Order By places umlauts & speical chars at end

眉间皱痕 提交于 2019-12-01 08:39:54
I'm using Phonegap to do a dictionary app for iOS. When querying the database for an alphabetical list I use COLLATE NOCASE : ORDER BY term COLLATE NOCASE ASC This solved the problem that terms starting with a lower case letter where appended to the end (Picked it up from that question ). However non-standard characters as öäüéêè still get sorted in the end ~ here 2 examples: Expected: Öffnungszeiten Oberved: Zuzahlung Zuzahlung Öffnungszeiten (or) clé cliquer sur cliquer sur clé I looked around and found similar matters discussed here or here but it seems the general advice is to install some

Syntax error near “ORDER BY order DESC” in MySQL?

守給你的承諾、 提交于 2019-12-01 08:13:35
Why I try to do an order by query, I always get an error telling me to check the syntax by the ORDER BY 'order' DESC.... Here's my query: SELECT * FROM posts ORDER BY order DESC; What am I doing wrong?? order is a reserved word in SQL; case does not matter. It must be quoted when used as an identifier . From the MySQL Reserved Words documentation: Certain words such as SELECT, DELETE, or BIGINT [or ORDER] are reserved and require special treatment for use as identifiers such as table and column names. Traditional MySQL quotes: SELECT * FROM posts ORDER BY `order` DESC; Proper (ANSI) SQL quotes

Problem joining tables where joined table needs to be ordered before grouping

走远了吗. 提交于 2019-12-01 08:07:50
问题 I have a scenario, which is seemingly simple on paper, but I'm having trouble getting to work as desired in practice. I have two tables (only relevant columns presented): | Thread +---------- | ThreadID | Post +---------- | PostID | ThreadID | Posted (Datetime) Now, what I want to do, is to join Thread and Post, grouping by ThreadID. But I want to order by Post.Posted in descending order. In plain english, I want to join Thread on the most recent Post relating to it. My SQL so far: SELECT

SQL: GROUP BY records and then get last record from each group? [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-01 07:45:51
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: SQL Server: Only last entry in GROUP BY I have a table like this: id| name | attendence 1 | Naveed| 1 2 | Naveed| 1 3 | Adil | 1 4 | Adil | 1 I use following query: SELECT * FROM `test` WHERE `attendence`=1 GROUP BY name The result with above query: id| name | attendence 3 | Adil | 1 1 | Naveed | 1 Question: Above result group rows by name but show first row from each group. I want to select last row(by id) from

Sorted difference between two columns

心不动则不痛 提交于 2019-12-01 07:05:56
问题 I have two columns (buying prince and sale price) and I want to calculate the difference between them. After that I want to order the result so I can see all the profit margins. Can I do it with just one SELECT statement? Thanks! 回答1: SELECT (sale_price - buy_price) AS profit FROM table_name ORDER BY profit DESC 回答2: Joe has it, but I think you might be looking for something slightly different for the ordering. Profit margin is defined as net income / revenue.. so the profit margin of each

How to work around ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc

北战南征 提交于 2019-12-01 06:40:46
问题 I want to lock one record in a table. The record is specified as "the next that has ID greater than..." CREATE TABLE test (id number); SELECT id FROM (SELECT id FROM test WHERE id > 10 ORDER BY id) WHERE ROWNUM = 1 FOR UPDATE; This seems intuitive and easy. But it is not. Any ideas? P.S. I do need the existing query to remain the same because it is a cursor and there are several places that use this cursor's %rowtype. 回答1: I think you're going to need something like: SELECT id FROM test WHERE

Mixing different categories results, ordered by score in MySQL

吃可爱长大的小学妹 提交于 2019-12-01 06:30:55
问题 In my PHP application, I have a mysql table of articles which has the following columns: article_id articletext category_id score Each article has a score which is calculated based on how popular it is, and belongs to a specific category (there are around 10 categories available) My question is: how can I perform a query that returns the highest scored articles while alternating them by categories so that if possible, no same-category articles are returned consecutively. For example if the

MySQL: ORDER BY with empty date '0000-00-00' as last but the rest ASC

北慕城南 提交于 2019-12-01 06:30:50
In our database, instead of making empty dates NULL , they are '0000-00-00' (I know, it sucks). How can I order these so that dates that are not '0000-00-00' are first and ordered ASC , and then the empty dates of '0000-00-00' come after? Thanks! ... ORDER BY CASE WHEN YourDateColumn = '0000-00-00' THEN 2 ELSE 1 END, YourDateColumn Try Below : SELECT * FROM your_table ORDER BY (date_column='0000-00-00'), date_column ASC OR select * from your_table order by if(date_column='0000-00-00',1,0),date_column; You can use a CASE WHEN http://dev.mysql.com/doc/refman/5.0/en/case-statement.html ... ORDER