sql-order-by

MySQL JOIN, GROUP BY, ORDER BY

▼魔方 西西 提交于 2019-12-05 08:10:23
I have a table of products: CREATE TABLE products (`id` INT); And a table of images for those products: CREATE TABLE images (`id` INT, `product_id` INT, `default` TINYINT(1)); I need to select all the products, and join the images table so that images with ( default = 1) will be preferred, and if a product has no images with ( default = 1), an image with ( default = 0) will be shown in its place. Here's an image showing what I'm looking for: Right now I have this query: SELECT p.id, i.id FROM products AS p LEFT JOIN ( SELECT product_id, url FROM images ORDER BY default ) AS i ON p.id = i

How to sort a collection based on a subcollection property

我的梦境 提交于 2019-12-05 07:58:28
I would like to sort a collection based on a subcollection property. //the subcollection public class Salary { public int SalaryId {get;set;} public int SalaryYear {get;set;} public double SalaryValue {get;set;} //this is the field we want to sort the parent collection "Person" } //the main collection public class Person { public int PersonId {get;set;} public string PersonName {get;set;} public List<Salary> Salaries {get;set;} } Below just for testing purpose, I'm preparing my collection of person with Salaries inner collections each one: List<Person> people = new List<Person>(); //add two

Mysql query which returns category tree

丶灬走出姿态 提交于 2019-12-05 07:53:35
I am working on category management. Where i have parentId with each row. Categories can be n-level. I can recursively call php function which will return category tree array. Now Issue is: In admin panel i want category listing page as shown below. i.e. which category is under which. Home Products (Edit) (Delete) Products > Product 1 (Edit) (Delete) Products > Product 2 (Edit) (Delete) Products > Product 2 > Product 2 1 (Edit) (Delete) Products > Product 2 > Product 2 2 (Edit) (Delete) Contact Us (Edit) (Delete) I want mysql query result in same order as shown above. I am not sure how can i

Order List by Date and Time (in string format)

大憨熊 提交于 2019-12-05 05:53:39
Probably what I'm asking is quite simple but I don't seem to be getting this one out. I have a list of elements containing a Date field and a Time field. The Date field is a regular DateTime and the Time field is a string. Time is formatted like HH:mm and ranges in 24h. Ordering my list by Date is simple by doing List.OrderBy(e => e.Date), but I don't seem to be able to later order it by Time so that the order of the records is according the date and the time. I tried this out but it's probably a big mistake! List = List.OrderBy(e => e.EstimatedDate).OrderBy(e => new TimeSpan(int.Parse(e

Django: how to order_by on a related field of a related field

跟風遠走 提交于 2019-12-05 05:03:09
I'm using annotate to add a property to an object which I can then use for order_by. However, I want to annotate on a field of a relation on a relation. I know I should be able to get to the field somehow using double-underscore notation, but I just can't seem to wrap my head around it. Here are the models: class Group(Taggable, Uploadable): name = models.CharField(max_length=250, db_index=True) description = models.TextField(max_length=5000, null=True, blank=True, db_index=True) private = models.BooleanField(default=False) members = models.ManyToManyField(User, null=True, related_name=

how to make sure a record is always at the top in a given resultset in mysql?

微笑、不失礼 提交于 2019-12-05 04:37:17
I have a search query which yields a resultset based on input parameters and the result can be sorted (ASC/DESC) based on different parameters: price, duration etc (with pagination in place and limit of 10 records) I now have a requirement wherein, if I have an id passed in, I'd like the corresponding record to be made to sticky at the top in the given resultset. Lets say we have a package table like this: Package - id - name - mPrice - vPrice - duration // Searching pkg based on Price (in DESC order) where name = Thailand sqlQuery = "SELECT p.id, p.name, p.mPrice, p.vPrice FROM package p

Is there a speed difference in ordering by int vs. float?

﹥>﹥吖頭↗ 提交于 2019-12-05 03:57:32
When retrieving entries in a database, is there a difference between storing values as a float or decimal vs. an int when using ORDERBY in a SELECT statement? It depends. You didn't specify the RDBMS so I can only speak to SQL Server specifically but data types have different storage costs associated with them. Ints range from 1 to 8 bytes, Decimals are 5-17 and floats are 4 to 8 bytes. The RDBMS will need to read data pages off disk to find your data (worst case) and they can only fit so many rows on an 8k page of data. So, if you have 17 byte decimals, you're going to get 1/17th the amount

GROUP BY ignoring an attribute

最后都变了- 提交于 2019-12-05 03:13:33
for example i have this table: itemgroup | description | price A, a, 10 A, b, 12 A, c, 14 B, g, 11 B, h, 16 now i want to select the rows with the highest price in one group like this: A, c, 14 B, h, 16 The SQL query (is fully functional) wich gets me near this is: SELECT itemgroup, MAX( price ) FROM table GROUP BY itemgroup A, 14 B, 16 By trying this I get an "not a GROUP BY expression"-error: SELECT itemgroup, description, MAX( price ) FROM table GROUP BY itemgroup I need something like this pseudo query: SELECT itemgroup, IGNORE( description), MAX( price ) FROM table GROUP BY itemgroup I

Prevent SQL Injection in ORDER BY clause

半世苍凉 提交于 2019-12-05 01:33:47
In our DB access layer we have some dynamic query creation. For instance, we have the following method for building a part of an ORDER BY clause: protected string BuildSortString(string sortColumn, string sortDirection, string defaultColumn) { if (String.IsNullOrEmpty(sortColumn)) { return defaultColumn; } return String.Format("{0} {1}", sortColumn, sortDirection); } The problem is, sortColumn and sortDirection both come from outside as strings, so of course something should be done to prevent possible injection attacks. Does anybody have any idea how this can be done? If you have to deal in

mysql SORT BY amount of unique word matches

邮差的信 提交于 2019-12-05 00:28:40
问题 I've found many questions that ask for amount of appearences, but none that ask the very same as I wish to do. A dynamically generated (prepared-statement) query will result in something like this: SELECT * FROM products WHERE ( title LIKE ? AND title LIKE ? ) AND ( content LIKE ? OR content LIKE ? ) AND ( subtitle LIKE ? AND author LIKE ? ) ORDER BY relevance LIMIT ?,? The amount of words entered, (and so the amount of LIKE ) are for title,content and author a variable amount (depending on