sql-order-by

mysql count group by order by optimization

≯℡__Kan透↙ 提交于 2019-11-30 23:02:14
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 to add index for tags, it seems with no effect. EDIT: EXPLAIN SELECT tags, COUNT(tags) as Num FROM

mysql order by issue

▼魔方 西西 提交于 2019-11-30 23:00:28
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 ? 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) ORDER BY CASE id WHEN 3 THEN 1 WHEN 6 THEN 2 WHEN 1 THEN 3 WHEN 8 THEN 4 WHEN 9 THEN 5 END I haven't tested but

How can I get just the first row in a result set AFTER ordering?

こ雲淡風輕ζ 提交于 2019-11-30 22:48:22
问题 This gives me just one row (the first one): SELECT BLA FROM BLA WHERE BLA AND ROWNUM < 2 However, I want the most recent date val; I can make that the first row this way: SELECT BLA FROM BLA WHERE BLA ORDER BY FINALDATE DESC When I try to mix the two partial victories, though, it doesn't work - apparently the "Select the first row only" logic fires before the order by, and then the order by is subsequently ignored. 回答1: This question is similar to How do I limit the number of rows returned by

Why does SQL Server 2008 order when using a GROUP BY and no order has been specified?

这一生的挚爱 提交于 2019-11-30 22:42:09
I'm running into a very strange issue that I have found no explanation for yet. With SQL Server 2008 and using the GROUP BY it is ordering my columns without any ORDER BY specified. Here is a script that demonstrates the situation. CREATE TABLE #Values ( FieldValue varchar(50) ) ;WITH FieldValues AS ( SELECT '4' FieldValue UNION ALL SELECT '3' FieldValue UNION ALL SELECT '2' FieldValue UNION ALL SELECT '1' FieldValue ) INSERT INTO #Values ( FieldValue ) SELECT FieldValue FROM FieldValues -- First SELECT demonstrating they are ordered DESCENDING SELECT FieldValue FROM #Values -- Second SELECT

How to match and sort by similarity in MySQL?

限于喜欢 提交于 2019-11-30 21:47:00
Currently, I am doing a search function. Lets say in my database, I have this data: Keyword1 Keyword2 Keyword3 Keysomething Key and the user entered: "Key" as the keyword to search. This is my current query: SELECT * FROM data WHERE ( data_string LIKE '$key%' OR data_string LIKE '%$key%' OR data_string LIKE '%$key' ) Basically, I have 2 questions: How do I sort by (order by) similarity. From above example, I wanted "Key" as my first result. My current result is: Keyword1, Keyword2, Keyword3, Keysomething and Key My SQL query only search by the "data_string" column, what if I want to seach

NHibernate HQL: left outer join with “with” clause does not work

别等时光非礼了梦想. 提交于 2019-11-30 20:34:56
In an EAV system, I have a mapping that looks like this: <class name="Record"> <map name="Values" table="RecordFieldValue"> <key column="RecordFK"> <index column="FieldFK"> <element column="Value"> </map> </class> I would like to select some Records, ordered by the value of each Record for a specific Field. However, note that not all Records will actually have a Value for that Field. In this case, the record should still be fetched and sorted with a null value. The desired SQL would look like this: select rec.*, val.Value from Record rec left outer join RecordFieldValue val on val.RecordFK =

SQL Query, Selecting 5 most recent in each group

三世轮回 提交于 2019-11-30 20:14:19
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `language_id` int(11) unsigned NOT NULL, `title` varchar(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `time_posted` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 language_id refers to what language the record is in. What I would like to do is retrieve a list of the five most recent (ORDER BY time_posted DESC LIMIT 5) records in each language_id . I could do this in a loop within PHP with a number of different SQL queries but I

MySQL select top rows with same condition values

时间秒杀一切 提交于 2019-11-30 19:17:29
I don't know how to title this problem. Correct me if you have better words. I have two tables, Users and Posts. Users: id | username | password | ... Posts: id | author_id | title | content | ... Now I want to list the "most active" users - the users who have written the most posts. And specifically, I want the top 10 result. SELECT u.username, COUNT(p.id) AS count FROM Posts p, Users u WHERE u.id=p.author_id GROUP BY p.author_id ORDER BY count DESC LIMIT 10; I can get the expected result. However, the ranking may not be " fair " if some users have same number of posts. E.g., I may get

Ordering distinct column values by (first value of) other column in aggregate function

◇◆丶佛笑我妖孽 提交于 2019-11-30 18:58:40
I'm trying to order the output order of some distinct aggregated text based on the value of another column with something like: string_agg(DISTINCT sometext, ' ' ORDER BY numval) However, that results in the error: ERROR: in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list I do understand why this is, since the ordering would be "ill-defined" if the numval of two repeated values differs, with that of another lying in-between. Ideally, I would like to order them by first appearance / lowest order-by value, but the ill-defined cases are actually rare enough in my

Why does SQL Server 2008 order when using a GROUP BY and no order has been specified?

时光毁灭记忆、已成空白 提交于 2019-11-30 18:25:19
问题 I'm running into a very strange issue that I have found no explanation for yet. With SQL Server 2008 and using the GROUP BY it is ordering my columns without any ORDER BY specified. Here is a script that demonstrates the situation. CREATE TABLE #Values ( FieldValue varchar(50) ) ;WITH FieldValues AS ( SELECT '4' FieldValue UNION ALL SELECT '3' FieldValue UNION ALL SELECT '2' FieldValue UNION ALL SELECT '1' FieldValue ) INSERT INTO #Values ( FieldValue ) SELECT FieldValue FROM FieldValues --