sql-order-by

MySQL JOIN, GROUP BY, ORDER BY

北慕城南 提交于 2019-12-10 04:38:50
问题 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

MYSQL Order By Sum of Columns

情到浓时终转凉″ 提交于 2019-12-10 03:39:53
问题 Any idea on how to order the results of a MYSQL query by the sum of two columns rather than by a single column? Select * FROM table ORDER BY (col1+col2) desc I know that won't work., but I hope it conveys what I want to do fairly well. Thanks! 回答1: Why not try before concluding it doesn't work? In point of fact, it does. 回答2: Suppose you have a table named ' Students ' Now you want to know the total marks scored by each student. So, type the following query SELECT Name, S1, S2, SUM(S1+S2) AS

In Django, how can I order by a multiple-choice CharField in arbitrary not alphabetical order?

独自空忆成欢 提交于 2019-12-10 02:09:55
问题 Imagine a model Shirts with a size CharField, with values limited to a small number of choices, e.g. 'small', 'medium', 'large', 'xlarge' etc. To get the shirts grouped by size, you'd do: Shirts.objects.order_by('size') But Django will (naturally) order the groups alphabetically, i.e. 'large' then 'medium' then 'small' then 'xlarge'. What I want is to have 'small' before 'medium' before 'large' etc. I.e. what I naturally want to do is something like the following pseudocode: size_order = {

LINQ - writing a query with distinct and orderby

时光总嘲笑我的痴心妄想 提交于 2019-12-10 02:06:32
问题 I'm quite new to LINQ. Suppose that I had the following table: Incident ID DeviceID Time Info 1 1 5/2/2009 d 2 2 5/3/2009 c 3 2 5/4/2009 b 4 1 5/5/2009 a In LINQ, how could I write a query that finds the most recent and distinct (on Device ID) set of incidents? The result I'd like is this: ID DeviceID Time Info 3 2 5/4/2009 b 4 1 5/5/2009 a Do you have to create an IEqualityComparer to do this? 回答1: You can get the most recent incidents for each device (this is how I understood your question)

MySQL - SELECT + JOIN + ORDER BY performance

折月煮酒 提交于 2019-12-09 19:41:03
问题 I'm having two tables, I need to select some data joined from both of them SELECT f.* FROM file_data f JOIN subscriptions s ON f.uid = s.elementid WHERE s.uid = 119762 AND f.private=0 ORDER BY f.date DESC Now, even for a small set of data the query takes over a second. This is due to 'filesort' and 'temporary' used on "subscriptions", caused by ORDER BY f.date (removing that condition causes the time to drop under 0.01s) Can anyone tell me how to speed up the query? Here's the result of

PHP/MySQL sort inside GROUP

半世苍凉 提交于 2019-12-09 18:58:38
问题 I have a case where I have to group results from table by brands and then order by some other column. I get my brands from an array. The problem is that when I group by brand this group is sorted by ID (inside group). Is there a way to sort (order by) inside group? Here is my array and mysql query. $laptop_brands = array("Acer", "Apple", "Dell", "HP-Compaq", "IBM-Lenovo", "Sony", "Toshiba", "ASUS", "Fujitsu", "Gateway"); $get_videos_query = "SELECT * FROM users_video WHERE location =

Laravel ordering results by specific values

孤街醉人 提交于 2019-12-09 18:21:47
问题 I have this line of code which gets results from a database: 'clanMembers' => User::find(Auth::user() -> clan_id) -> where('clan_id', '=', Auth::user() -> clan_id) -> orderBy('username') -> get() Currently it orders by the username. I wish to change this to order by the clan_rank field. This clan_rank field will only ever contain 3 different values. These are: 1. Owner 2. Admin 3. Member I wish to have my results ordered with Owners first, then Admin, then members. How can I change my line of

How to have a custom sort order for a union query in Postgres

邮差的信 提交于 2019-12-09 15:09:58
问题 With a query like this (simplified for clarity): SELECT 'East' AS name, * FROM events WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' UNION SELECT 'West' AS name, * FROM events WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' UNION SELECT 'Both' AS name, * FROM events WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' I want to customise the order of the resulting rows. Something like: ORDER BY name='East',

LINQ Queries with dynamic Order By

我们两清 提交于 2019-12-09 09:49:50
问题 I have a query where I need to have ordeby based on a querystring parameter .For example if sortby parameter is price , Query needs to change with price . If its rating than change query to sort by rating . I know PredicateBuilder can do And and OR stuff but how do I make a dynamic ordeby linq query . 回答1: Well, you could use a switch statement or something similar: IQueryable<Foo> query = ...; switch (orderByParameter) { case "price": query = query.OrderBy(x => x.Price); break; case "rating"

Why does added RAND() cause MySQL to overload?

孤人 提交于 2019-12-09 04:04:26
OK I have this query which gives me DISTINCT product_series, plus all the other fields in the table: SELECT pi.* FROM ( SELECT DISTINCT product_series FROM cart_product ) pd JOIN cart_product pi ON pi.product_id = ( SELECT product_id FROM cart_product po WHERE product_brand = "everlon" AND product_type = "'.$type.'" AND product_available = "yes" AND product_price_contact = "no" AND product_series != "" AND po.product_series = pd.product_series ORDER BY product_price LIMIT 1 ) ORDER BY product_price This works fine. I am also ordering by price so I can get the starting price for each series.