sql-order-by

Sorting related items in a Django template

霸气de小男生 提交于 2019-11-27 03:35:29
Is it possible to sort a set of related items in a DJango template? That is: this code (with HTML tags omitted for clarity): {% for event in eventsCollection %} {{ event.location }} {% for attendee in event.attendee_set.all %} {{ attendee.first_name }} {{ attendee.last_name }} {% endfor %} {% endfor %} displays almost exactly want I want. The only thing I want to change is I the list of attendees to be sorted by last name. I've tried saying something like this: {% for event in events %} {{ event.location }} {% for attendee in event.attendee_set.order_by__last_name %} {{ attendee.first_name }}

MYSQL Select One Random record from each Category

大城市里の小女人 提交于 2019-11-27 03:34:04
问题 I have a database with an Items table that looks something like this: id name category (int) There are several hundred thousand records. Each item can be in one of 7 different categories , which correspond to a categories table: id category I want a query that chooses 1 random item, from each category. Whats the best way of approaching that? I know to use Order By rand() and LIMIT 1 for similar random queries, but I've never done something like this. 回答1: This query returns all items joined

What is the MS SQL Server capability similar to the MySQL FIELD() function?

懵懂的女人 提交于 2019-11-27 02:41:05
问题 MySQL provides a string function named FIELD() which accepts a variable number of arguments. The return value is the location of the first argument in the list of the remaining ones. In other words: FIELD('d', 'a', 'b', 'c', 'd', 'e', 'f') would return 4 since 'd' is the fourth argument following the first. This function provides the capability to sort a query's results based on a very specific ordering. For my current application there are four statuses that I need to manager: active,

SQLAlchemy: How to order query results (order_by) on a relationship's field?

谁说胖子不能爱 提交于 2019-11-27 02:33:59
问题 Models from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, ForeignKey from sqlalchemy import Integer from sqlalchemy import Unicode from sqlalchemy import TIMESTAMP from sqlalchemy.orm import relationship BaseModel = declarative_base() class Base(BaseModel): __tablename__ = 'base' id = Column(Integer, primary_key=True) location = Column(Unicode(12), ForeignKey("locationterrain.location"), unique=True,) name = Column(Unicode(45)) ownerid = Column(Integer

Why do NULL values come first when ordering DESC in a PostgreSQL query?

霸气de小男生 提交于 2019-11-27 02:09:32
When would you ever want NULLS first when ordering a query descending or ascending? In my opinion, the vast majority of the time the desired behavior whether sorting ascending or descending would be NULLS LAST. Instead, we should have to specify NULLS FIRST. Erwin Brandstetter Actually, with default sort order ( ASCENDING ) NULL values come last . Logic dictates that the sort order be reversed with the DESCENDING keyword, so NULLs come first in this case. But the best part comes last: you can choose which way you want it: Use the NULLS FIRST | LAST clause. Quoting the current manual , version

mysql get table column names in alphabetical order

白昼怎懂夜的黑 提交于 2019-11-27 01:48:31
问题 Is it possible to query a MySQL database to get the column names of a table in alphabetical order? I know that SHOW COLUMNS `table_name`; or DESCRIBE `table_name`; will give me a list of the columns in a table (along with other info), but is it possible to alter the query in order to get the columns sorted alphabetically. Adding ORDER BY 'Field' didn't work, it gave a syntax error. 回答1: The ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS) provide more flexibility in

How to order by count in Doctrine 2?

夙愿已清 提交于 2019-11-27 01:37:42
问题 I'm trying to group my entity by a field (year) and do a count of it. Code: public function countYear() { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('b.year, COUNT(b.id)') ->from('\My\Entity\Album', 'b') ->where('b.year IS NOT NULL') ->addOrderBy('sclr1', 'DESC') ->addGroupBy('b.year'); $query = $qb->getQuery(); die($query->getSQL()); $result = $query->execute(); //die(print_r($result)); return $result; } I can't seem to say COUNT(b.id) AS count as it gives an error,

SQL ORDER chars numerically

流过昼夜 提交于 2019-11-27 01:28:09
问题 I have a column of numbers stored as chars. When I do a ORDER BY for this column I get the following: 100 131 200 21 30 31000 etc. How can I order these chars numerically? Do I need to convert something or is there already an SQL command or function for this? Thank You. 回答1: Try this: ORDER BY CAST(thecolumn AS int) 回答2: This Worked for me: ORDER BY ABS(column_name) 回答3: This is an issue with ordering numeric strings in a "natural sort" (if you lookup "natural sorting" on Google you'll find

Custom sort logic in OrderBy using LINQ

落花浮王杯 提交于 2019-11-27 01:07:07
What would be the right way to sort a list of strings where I want items starting with an underscore '_', to be at the bottom of the list, otherwise everything is alphabetical. Right now I'm doing something like this, autoList.OrderBy(a => a.StartsWith("_") ? "ZZZZZZ"+a : a ) If you want custom ordering, but don't want to supply a comparer, you can have it - sql style: autoList .OrderBy(a => a.StartsWith("_") ? 2 : 1 ) .ThenBy(a => a); I think you need to use OrderBy(Func<>, IComparer<>) and specify your own Comparer which will implement your custom logic . Use the overload of OrderBy that

SQLite and custom order by

徘徊边缘 提交于 2019-11-27 00:59:25
I have a table with categories: ID Category "1","Baking" "3","Family" "4","Entertaining" "5","Children" "6","Desserts" Now I would like to order the result of the select statement to ID Category "4","Entertaining" "3","Family" "1","Baking" "5","Children" "6","Desserts" for example. In MySQL, you'd do it like this: SELECT * FROM CATEGORIES ORDER BY FIELD (ID, 4,3,1,5,6); How would you do it in SQLite though? I don't have an "order by" field. ORDER BY CASE ID WHEN 4 THEN 0 WHEN 3 THEN 1 WHEN 1 THEN 2 WHEN 5 THEN 3 WHEN 6 THEN 4 END A second way of doing it (the first one being with CASE WHEN ...