sql-order-by

SQLITE order by numeric and not alphabetic

孤街醉人 提交于 2020-08-08 07:15:09
问题 When I order my database SQLITE by Classement I have this : Classement | Nom 1 | clem 10 | caro 11 | flo 12 | raph 2 | prisc 3 | karim 4 | prout I would like to get : Classement | Nom 1 | clem 2 | prisc 3 | karim 4 | prout 10 | caro 11 | flo 12 | raph Here is my code : SELECT t.Classement FROM tableau t WHERE 1 = (SELECT 1 + COUNT (*) FROM tableau t2 WHERE t2.Classement < t.Classement OR ( t2.Classement == t.Classement AND t2.Nom < t.Nom )) Can anyone help me ? Thank you! 回答1: You get

Greater than or equal to ALL() and equal to MAX() speed

一个人想着一个人 提交于 2020-08-05 09:59:09
问题 I have a relation in PostgreSQL named product which contains 2 fields: id and quantity , and I want to find the id of the products with the highest quantity . As far as I know, there are 2 ways of doing it: SELECT id FROM product WHERE quantity >= ALL(SELECT quantity FROM product) or SELECT id FROM product WHERE quantity = (SELECT MAX(quantity) FROM product) Is there any difference in their speed of execution? 回答1: The first query fails if any row has quantity IS NULL values (as Gordon

Order by Column1 if Column1 is not null, otherwise order by Column2

点点圈 提交于 2020-08-02 06:15:10
问题 Is there a way to combine ORDER BY and IS NULL in sql so that I can order by a column if the column isn't null, but if it is null, order by another column? 回答1: Something like: ORDER BY CASE WHEN Column1 IS NOT NULL THEN Column1 ELSE Column2 END Same as writing: ORDER BY COALESCE(Column1, Column2) Both should work in any sane RDBMS. 回答2: Try this ORDER BY COALESCE(fieldA, fieldB); 回答3: I dont have any Tables atm where I could test it, but this may work, at least it did without useable data:

ORDER BY date with past dates after upcoming dates

删除回忆录丶 提交于 2020-07-06 09:33:18
问题 I need do execute a query on a table in a MySql database where the order of the resulting rows will be like this: If today is 10/09/12: ... 11/09/12 12/09/12 15/09/12 08/09/12 <--here start the past dates 07/09/12 05/09/12 .... Is there a way to achive this directly in MySQL? I've resolved in this way: First, the select statement include a new boolean that mark if the date is past or future by: SELECT DISTINCT *,CASE WHEN startdate < CURDATE() THEN 0 ELSE 1 END AS past_or_future Second, I've

How to increment column by one group wise

最后都变了- 提交于 2020-06-16 03:35:33
问题 I have a table called productLocation , and its data structure as follows, SQLFiddle +--------------+-------------+-----------+ | FkLocationId | FkProductId | SortValue | +--------------+-------------+-----------+ | 1 | 100 | 0 | +--------------+-------------+-----------+ | 1 | 101 | 0 | +--------------+-------------+-----------+ | 1 | 102 | 0 | +--------------+-------------+-----------+ | 1 | 103 | 2 | +--------------+-------------+-----------+ | 1 | 104 | 1 | +--------------+-------------+-

PostgreSQL ORDER BY values in IN() clause

↘锁芯ラ 提交于 2020-06-08 17:28:01
问题 Ok, there are some answers out there on how to do this. But all of the answers are assuming that the query is selecting all. If you have a distinct select, the methods no longer work. See here for that method: Simulating MySQL's ORDER BY FIELD() in Postgresql Basically I have SELECT DISTINCT id FROM items WHERE id IN (5,2,9) ORDER BY CASE id WHEN 5 THEN 1 WHEN 2 THEN 2 WHEN 9 THEN 3 END Of course, this breaks and says "PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in

PostgreSQL - conditional ordering

◇◆丶佛笑我妖孽 提交于 2020-05-29 07:08:11
问题 I have the following table: key | date | flag -------------------------- 1 now() true 2 now() - 1 hour true 3 now() + 1 hour true 4 now() false 5 now() - 1 hour false 6 now() + 1 hour false I want the following sorting: First, all rows with flag = false . These rows must be sorted with date asc . Then, all other rows ( flag = true ). However, these rows must be sorted with date desc . Is the following query correct? ( select * from test where flag = false order by date asc ) union all (

mysql: very simple SELECT id ORDER BY LIMIT will not use INDEX as expected (?!)

放肆的年华 提交于 2020-05-14 03:42:05
问题 I have a simple table with about 3 million records. I made the neccessary indexes, i also force the index PRIMARY but still doesnt work. It searches for nearly all 3 million rows instead of using the index to execute this one (record_id is INT auto-increment): EXPLAIN SELECT record_id FROM myrecords FORCE INDEX ( PRIMARY ) ORDER BY record_id ASC LIMIT 2955900 , 300 id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE myrecords index NULL PRIMARY 4 NULL 2956200 Using

Multiple LIKE Operator ORDER BY Strongest

怎甘沉沦 提交于 2020-05-08 19:56:08
问题 I have the following query: SELECT * FROM table_name WHERE (genre LIKE '%romance%' OR genre LIKE '%comedy%' OR genre LIKE '%horror%') ORDER BY *the column that has more* OR SOMETHING LIKE THAT $sql = "SELECT * FROM table WHERE (genre LIKE '%romance%' AND genre LIKE '%comedy%' AND genre LIKE '%horror%') #If result < 12 (genre LIKE '%romance%' AND genre LIKE '%comedy%') OR (genre LIKE '%romance%' AND genre LIKE '%horror%') OR (genre LIKE '%comedy%' AND genre LIKE '%horror%') #If result < 12

How to update SQL Server Table after query sort by datetime

帅比萌擦擦* 提交于 2020-04-18 03:46:40
问题 I'm using a SQL Server database and I have a datetime column, datatype is datetime , now I already know how to sort table by datetime: SELECT * FROM [database].[dbo].[data] ORDER BY [datetime]; Result: datetime 2020-03-18 09:18:00 2020-03-18 09:19:00 2020-03-18 09:20:00 2020-03-18 09:21:00 ............. ............. ............. My question is after the above step that query sort it by datetime, how can I update and save the table? Because I want to keep or the data sort by datetime. Can