sql-order-by

Entity Framework Include OrderBy random generates duplicate data

荒凉一梦 提交于 2019-12-17 07:16:25
问题 When I retrieve a list of items from a database including some children (via .Include), and order the randomly, EF gives me an unexpected result.. I creates/clones addition items.. To explain myself better, I've created a small and simple EF CodeFirst project to reproduce the problem. First i shall give you the code for this project. The project Create a basic MVC3 project and add the EntityFramework.SqlServerCompact package via Nuget. That adds the latest versions of the following packages:

Returning query results in predefined order

∥☆過路亽.° 提交于 2019-12-17 06:41:09
问题 Is it possible to do a SELECT statement with a predetermined order, ie. selecting IDs 7,2,5,9 and 8 and returning them in that order , based on nothing more than the ID field? Statements SELECT id FROM table WHERE id in (7,2,5,9,8); and SELECT id FROM table WHERE id in (8,2,5,9,7); both return them in the same order. 回答1: I didn't think this was possible, but found a blog entry here that seems to do the type of thing you're after: SELECT id FROM table WHERE id in (7,2,5,9,8) ORDER BY FIND_IN

Mysql: Order by like?

♀尐吖头ヾ 提交于 2019-12-17 06:29:34
问题 assume that we are performing search using keywords: keyword1, keyword2, keyword3 there are records in database with column "name": 1: John Doe 2: Samuel Doe 3: John Smith 4: Anna Smith now Query: SELECT * FROM users WHERE (name LIKE "%John%" OR name LIKE "%Doe%") it will select records: 1,2,3 (in this order) but i want to order it by keyword in example keyword1=John, keyword2=Doe so it should be listed by keywords: 1,3,2 (because i want to perform search for "Doe" after searching for "John")

Order by COUNT per value

被刻印的时光 ゝ 提交于 2019-12-17 06:04:07
问题 I have a table which stores IDs and the city where the store is located. I want to list all the stores starting with the stores that are in the city where there are the most stores. TABLE ID CITY 1 NYC 2 BOS 3 BOS 4 NYC 5 NYC The output I want is the following since I have the most stores in NYC, I want all the NYC location to be listed first. 1 NYC 4 NYC 5 NYC 2 BOS 3 BOS 回答1: SELECT count(City), City FROM table GROUP BY City ORDER BY count(City); OR SELECT count(City) as count, City FROM

Linq order by, group by and order by each group?

蹲街弑〆低调 提交于 2019-12-17 05:47:04
问题 I have an object that looks something like this: public class Student { public string Name { get; set; } public int Grade { get; set; } } I would like to create the following query: group grades by student name, order each student group by grades, and order groups by max grade in each group. So it will look like this: A 100 A 80 B 80 B 50 B 40 C 70 C 30 I created the following query: StudentsGrades.GroupBy(student => student.Name) .OrderBy(studentGradesGroup => studentGradesGroup.Max(student

How to ORDER BY a SUM() in MySQL?

我怕爱的太早我们不能终老 提交于 2019-12-17 05:03:41
问题 I have a table: "ID name c_counts f_counts " and I want to order all the record by sum(c_counts+f_counts) but this doesn't work: SELECT * FROM table ORDER BY sum(c_counts+f_counts) LIMIT 20; 回答1: Don'y forget that if you are mixing grouped (ie. SUM) fields and non-grouped fields, you need to GROUP BY one of the non-grouped fields. Try this: SELECT SUM(something) AS fieldname FROM tablename ORDER BY fieldname OR this: SELECT Field1, SUM(something) AS Field2 FROM tablename GROUP BY Field1 ORDER

MySQL Order before Group by

倾然丶 夕夏残阳落幕 提交于 2019-12-17 04:28:19
问题 I need to find the latest post for each author and then group the results so I only a single latest post for each author. SELECT wp_posts.* FROM wp_posts WHERE wp_posts.post_status='publish' AND wp_posts.post_type='post' GROUP BY wp_posts.post_author ORDER BY wp_posts.post_date DESC This is correctly grouping the output so I only get one post per author, but it is ordering the results after they have been grouped and not before they have been selected. 回答1: select wp_posts.* from wp_posts

MySQL Order before Group by

回眸只為那壹抹淺笑 提交于 2019-12-17 04:28:17
问题 I need to find the latest post for each author and then group the results so I only a single latest post for each author. SELECT wp_posts.* FROM wp_posts WHERE wp_posts.post_status='publish' AND wp_posts.post_type='post' GROUP BY wp_posts.post_author ORDER BY wp_posts.post_date DESC This is correctly grouping the output so I only get one post per author, but it is ordering the results after they have been grouped and not before they have been selected. 回答1: select wp_posts.* from wp_posts

sql ORDER BY multiple values in specific order?

余生颓废 提交于 2019-12-17 03:30:19
问题 Ok I have a table with a indexed key and a non indexed field. I need to find all records with a certain value and return the row. I would like to know if I can order by multiple values. Example: id x_field -- ----- 123 a 124 a 125 a 126 b 127 f 128 b 129 a 130 x 131 x 132 b 133 p 134 p 135 i pseudo: would like the results to be ordered like this, where ORDER BY x_field = 'f', 'p', 'i', 'a' SELECT * FROM table WHERE id NOT IN (126) ORDER BY x_field 'f', 'p', 'i', 'a' So the results would be:

How can I do an OrderBy with a dynamic string parameter?

穿精又带淫゛_ 提交于 2019-12-17 00:52:33
问题 I want to do this: var orderBy = "Nome, Cognome desc"; var timb = time.Timbratures.Include("Anagrafica_Dipendente") .Where(p => p.CodDipendente == 1); if(orderBy != "") timb = timb.OrderBy(orderBy); Is there an OrderBy overload available that accepts a string parameter? 回答1: Absolutely. You can use the LINQ Dynamic Query Library, found on Scott Guthrie's blog. There's also an updated version available on CodePlex. It lets you create OrderBy clauses, Where clauses, and just about everything