sql-order-by

SQL syntax error at or near 'union'

不想你离开。 提交于 2019-12-05 17:07:33
问题 I have a small query, and a union to put another small query next to it. However, the union has a syntax error in it. Select <column1> ,<column2> From <Table1> <Some joins in there> Where <conditions> group by <column2> order by <column2> union select <column2> ,<column3> ,<column4> From <Table2> <Some more joins here> Where <conditions> group by <column2> order by <column2> This is the Error I receive ERROR: Syntax error at or near 'union' 回答1: I see what was wrong. You have to place the

MySQL Order from a starting value

时光总嘲笑我的痴心妄想 提交于 2019-12-05 16:08:23
On a product page I have a dropdown that lists current colour options associated to product page. In this example, the product page SKU is 250E and it available in: GREEN BLACK If the customer selects GREEN, then I want to run a MySQL command that will change the data to show GREEN values first based in the custom_order value shown below. The start value should overide the other data items and then it should retain custom_order values. The custom_order field has letters like c1, c2 (they will always be at the bottom) Colour Table ============ ID COLOURID NAME ----------------------------------

Order resultset based on WHERE IN clause data

最后都变了- 提交于 2019-12-05 15:53:47
Considering this MySQL query: SELECT someColumns FROM someTable WHERE someColumn IN ( value1, value2, value3 ) ... how can I guarantee that the rowset comes out ordered in the exact order of the values given to the IN () clause? I presume this is not guaranteed without giving it an ORDER BY clause, is it? PS.: The values to the IN () clause will be an array of arbitrary data passed to the query by PHP (utilizing Zend Framework's select statement) in the following manner: ->where( 'someColumn in (?)', $theArrayWithValues ); Use a CASE statement in the ORDER BY: ORDER BY CASE someColumn WHEN

“Invalid column name”, Order By on Alias of subquery

早过忘川 提交于 2019-12-05 15:46:43
I've created a stored procedure where i want to add an alternative order by clause. The problen is that the query failed on a "Invalid column name 'aantal regels'" Here is the query I have now. SELECT l.lead_id, l.afdeling_id, l.advertentie_id, l.naam, l.type, l.status, l.herkomst, l.aanmaakdatum, l.klant_id, l.overigegegevens, af.afdelingsnaam, (SELECT COUNT(lead_regel_id) FROM Lead_regel As lr Where Lr.lead_id = l.lead_id And lr.status <> 100 ) AS aantal_regels, (SELECT COUNT(lead_id) FROM Lead_unread As lu Where lu.lead_id = l.lead_id And lu.user_id = @uid ) As lead_ongelezen, (SELECT COUNT

Can not ORDER BY an AVG value with certain GROUP BY criteria in MySQL

浪子不回头ぞ 提交于 2019-12-05 13:18:49
I have a table data_summaries . It has such columns as item_id INT(11) , user_grouping TEXT and value DECIMAL(10,2) . If I try to make a query that groups the results by user_grouping and orders them by the AVG of value , that fails: SELECT user_grouping, AVG(value) AS avg_value, SUM(value) AS sum_value FROM data_summaries GROUP BY user_grouping ORDER BY avg_value +---------------+-----------+-----------+ | user_grouping | avg_value | sum_value | +---------------+-----------+-----------+ | London | 50.609733 | 18978.65 | | Paris | 50.791733 | 19046.90 | | New York | 51.500400 | 2575.02 | |

ORDER BY Color with Hex Code as a criterio in MySQL

眉间皱痕 提交于 2019-12-05 12:19:42
I have a table that contains color options for a product. The color options include a hex color code, which is used to generate the UI (HTML). I would like to sort the rows so that the colors in the UI look like a rainbow, instead of the current order that sorts based off of the Name of the color (not very useful). Here is what my query looks like. I get the R G B decimal values from the hex code. I just don't know how to order it. I've looked into color difference algorithms. They seem more useful to compare 2 colors' similarity, not sort. I'm using MySQL: select a.*, (a.c_r + a.c_g + a.c_b)

MS Sql: Conditional ORDER BY ASC/DESC Question

回眸只為那壹抹淺笑 提交于 2019-12-05 11:58:07
问题 I want to to make to make the ordering in my query conditional so if it satisfiess the condition it should be ordered by descending For instance: SELECT * FROM Data ORDER BY SortOrder CASE WHEN @Direction = 1 THEN DESC END 回答1: Don't change the ASC or DESC , change the sign of the thing being sorted-by: SELECT * FROM table ORDER BY CASE WHEN @Direction = 1 THEN -id else id END asc; The OP asks: Guys, I am not the SQL Expert, please explain me what means the id and -id, does it controls the

order by within group concat

大憨熊 提交于 2019-12-05 11:47:39
The order by is not working in the second query. I need to order by first DNAID then DNBID First Query its ordered as: 111221 Second Query its ordered as: 112112 for more info and details about what im trying to accomplish https://stackoverflow.com/questions/5082880/database-query-group-union-find-latest mysql> select * from metarun; +----+------------+-------+-------+--------------+----------+ | ID | RunGroupID | DNAID | DNBID | CONFIGTYPEID | DateTime | +----+------------+-------+-------+--------------+----------+ | 1 | 1 | 1 | 1 | 2 | NULL | | 2 | 1 | 1 | 2 | 2 | NULL | | 3 | 1 | 2 | 1 | 2

MySQL group by with ordering/priority of another column

若如初见. 提交于 2019-12-05 10:47:53
I've already looked at these two questions: Grouping by Column with Dependence on another Column MySQL GROUP BY with preference However both of them use an aggregate function MAX in order to obtain the highest or filled in value, which doesn't work for my case. For the purposes of this question, I've simplified my situation. Here is my current data: I'd like to obtain the operator name for each route, but with respect to direction of travel (i.e. ordering or "preferring" values). This is my pseudo-code: if(`direction` = 'west' AND `operatorName` != '') then select `operatorName` else if(

Order by field in cakephp

牧云@^-^@ 提交于 2019-12-05 10:20:36
问题 I am doing project in cakephp . I want to write below query in cakephp Style. I've written 50% . Please help me $this->Login->find('all') SELECT * FROM login ORDER BY FIELD(profile_type, 'Basic', 'Premium') DESC; 回答1: Plese try this $this->Login->find('all', array( 'order'=>array('FIELD(Login.profile_type, "basic", "premium") DESC') )); 回答2: You can pass options to the find method: $this->Login->find('all', array( 'order' => "FIELD(Login.profile_type, 'Basic', 'Premium') DESC" )); 回答3: Please