sql-order-by

mysql count group by order by optimization

ぐ巨炮叔叔 提交于 2019-11-30 18:00:36
问题 I have a table to store tag name for posts table: tagname tags | pid festival | 10034 New York Fashion Week | 10034 festival | 10035 car | 10036 ... The table now has already 590,000 records. Now I want to get the first 10 most popular tags from this table. SELECT tags, COUNT(*) as Num FROM tagname GROUP BY tags ORDER BY Num DESC This will cost 23.88 seconds. return 358 kinds of tags tags | Num festival | 7201 art | 6988 gift | 6755 ... How to optimization this query, even in my.cnf ? I tried

SQL ORDER by `no` with NULLs at the end

六眼飞鱼酱① 提交于 2019-11-30 17:42:05
I have got a MySql query that orders me results by no column (int, can be null). Simple example: SELECT * FROM table ORDER BY no ASC I would like to get a resultset sorted like 1, 2, 3, 10, 52, 66, NULL, NULL, NULL but I get NULL, NULL, NULL, 1, 2, 3, 10, 52, 66 Is it possible with SQL query ? Could you try this? ORDER BY ISNULL(no),no; You can use a CASE statement to tweak ordering: SELECT * FROM table ORDER BY case when no is null then 2 else 1 end, no This orders on "nullableness" first, and no second. Thompson SELECT * FROM table ORDER BY ISNULL(field), field ASC; SELECT * FROM table ORDER

mysql order by issue

情到浓时终转凉″ 提交于 2019-11-30 17:33:34
问题 if i have a query like : SELECT * FROM table WHERE id IN (3,6,1,8,9); this array of the ids is build in php dynamically , and the order is important to me. $my_array = array (3,6,1,8,9) ; how can i sort the results by the order by which the elements appear in my array ? its possible to do it in MYSQL query, or i must to order it after via php ? 回答1: You can order by a value derived from a column. You can use a CASE operator to specify the order: SELECT * FROM table WHERE id IN (3,6,1,8,9)

SQL: Using Top 1 in UNION query with Order By

Deadly 提交于 2019-11-30 17:16:17
I have a table as below Rate Effective_Date ---- -------------- 5.6 02/02/2009 5.8 05/01/2009 5.4 06/01/2009 5.8 12/01/2009 6.0 03/15/2009 I am supposed to find the all rates that are effective for current date and after it. So to get the current effective rate, i use SELECT TOP 1 * from table where effective_date < '05/05/2009' order by effective date desc for the rates after the current date the query is SELECT * from table where effective_date > '05/05/2009' To combine these two result i use a union as SELECT TOP 1 * from table where effective_date < '05/05/2009' order by effective date

MYSQL order by time am/pm

删除回忆录丶 提交于 2019-11-30 15:30:39
I have a row in a table which holds the time of day in am/pm format e.g. timeField --------- 9.00am 10.00pm 7.00am etc... Is there a way in mysql to order these values? You can do this by using STR_TO_DATE function in MySQL: SELECT STR_TO_DATE('10.00pm','%h.%i%p'); try this: SELECT * FROM table_name ORDER BY STR_TO_DATE(timeField,'%h.%i%p'); Example: SQLFiddle In order to order it you need to format the date field fist. try this SELECT COl1, COl2, DATE_FORMAT(yourdate, '%k:%i:%s') AS mydate FROM your_table ORDER BY mydate Enjoy SELECT * FROM table ORDER BY Name This put everything in order for

Is there a way in SQL (MySQL) to do a “round robin” ORDER BY on a particular field?

风流意气都作罢 提交于 2019-11-30 14:33:30
Is there a way in SQL (MySQL) to do a "round robin" ORDER BY on a particular field? As an example, I would like to take a table such as this one: +-------+------+ | group | name | +-------+------+ | 1 | A | | 1 | B | | 1 | C | | 2 | D | | 2 | E | | 2 | F | | 3 | G | | 3 | H | | 3 | I | +-------+------+ And run a query that produces results in this order: +-------+------+ | group | name | +-------+------+ | 1 | A | | 2 | D | | 3 | G | | 1 | B | | 2 | E | | 3 | H | | 1 | C | | 2 | F | | 3 | I | +-------+------+ Note that the table may have many rows, so I can't do the ordering in the application

MySQL order by relevance

给你一囗甜甜゛ 提交于 2019-11-30 13:09:57
I have a search form which searches a site content table to pull back appropriate results. I want to search the title and content fields and pull back results in order of relevance. Giving highest priority to the title. Say we had a table (tblContent) of intID | strTitle | txtContent 1 | Smith John | Lorem Ipsum 2 | Lorem Ipsum | Lorem John Smith Ipsum 3 | John Smith | Lorem Ipsum Lorem Ipsum 4 | Lorem Ipsum | Lorem Ipsum Lorem Ipsum 5 | Lorem Ipsum | Lorem Ipsum Smith John And you were searching for "John Smith" the results should come back in the order of 3,2,1,5 How is this possible? I

Select query using IN() and without any sorting

隐身守侯 提交于 2019-11-30 13:09:56
问题 My query select * from product where productId in(25,36,40,1,50); Result shows as follows `productId ProductName Qty Price` ------------------------------------- `1 | namesome | 5 | 25.00` `25 | namesome | 5 | 35.00` `36 | namesome | 5 | 35.00` `40 | namesome | 5 | 35.00` `50 | namesome | 5 | 35.00` I did not use any order by clause, But its automatically applied order by productId , I need result with out any sort, as follows `productId ProductName Qty Price` --------------------------------

What can an aggregate function do in the ORDER BY clause?

妖精的绣舞 提交于 2019-11-30 11:59:53
Lets say I have a plant table: id fruit 1 banana 2 apple 3 orange I can do these SELECT * FROM plant ORDER BY id; SELECT * FROM plant ORDER BY fruit DESC; which does the obvious thing. But I was bitten by this, what does this do? SELECT * FROM plant ORDER BY SUM(id); SELECT * FROM plant ORDER BY COUNT(fruit); SELECT * FROM plant ORDER BY COUNT(*); SELECT * FROM plant ORDER BY SUM(1) DESC; All these return just the first row (which is with id = 1). What's happening underhood? What are the scenarios where aggregate function will come in handy in ORDER BY ? Your results are more clear if you

Using MySql, can I sort a column but have 0 come last?

♀尐吖头ヾ 提交于 2019-11-30 11:45:47
问题 I want to sort by an column of ints ascending, but I want 0 to come last. Is there anyway to do this in MySql? 回答1: You may want to try the following: SELECT * FROM your_table ORDER BY your_field = 0, your_field; Test case: CREATE TABLE list (a int); INSERT INTO list VALUES (0); INSERT INTO list VALUES (0); INSERT INTO list VALUES (0); INSERT INTO list VALUES (1); INSERT INTO list VALUES (2); INSERT INTO list VALUES (3); INSERT INTO list VALUES (4); INSERT INTO list VALUES (5); Result: SELECT