sql-order-by

How to return records in correct order in PostgreSQL

帅比萌擦擦* 提交于 2019-12-11 02:29:50
问题 Query select 'T' union all select 'Z' order by 1 returns Z T but correct order should be T Z Tried in 3 different OSes Windows 2003 server "PostgreSQL 9.1.3, compiled by Visual C++ build 1500, 32-bit" Windows 7 "PostgreSQL 9.2.1, compiled by Visual C++ build 1600, 32-bit" Debian "PostgreSQL 9.1.2 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Debian 4.4.5-8) 4.4.5, 64-bit" Database settings are: Collation: et_EE.UTF-8 Character type: et_EE.UTF-8 How to fix this? Update Databases were

Mysql - update table column from another column based on order

梦想的初衷 提交于 2019-12-11 02:16:35
问题 I have a table with columns id, name, position, old_position . The column old_position contains unique numbers that are used to define the order when making select. But the numbers are not consecutive(even if when ordered). E.g. 2, 12, 11, 14, 20, 35, 45, 28, What I am trying to achieve is to populate the column position with consecutive numbers starting from 1, but with the same order according to old_position, so when I order by position ASC still I can get the same order of rows. Is there

ordering by collection count in nhibernate

坚强是说给别人听的谎言 提交于 2019-12-11 02:02:15
问题 Is there a way of ordering a list of objects by a count of a property which is a collection? For arguments sake let's say I have a question object with a question name property, a property that is a collection of answer objects and another property that is a collection of user objects. The users join the question table via foreign key on question table and answers are joined with middle joining table. If I want nhibernate to get a list of "question" objects could I order it by Question

Query MSQL for winners, starting at xth place using SELECT

最后都变了- 提交于 2019-12-11 01:55:53
问题 In my MySQL table Winners , I have a list of people who have won. What I'd like to do is select a list of the names of 10 winners. So what I have right now is this: SELECT name FROM Winners ORDER BY points DESC LIMIT 10 This returns the first 10 winners which is great. But how can I make it (for example) return 10 winners, but starting at 20th place? Right now all I can think of is removing the LIMIT and programatically pulling out the 10 winners I want. But I'm sure there's an easier way.

Json_encode changing the order of my query

Deadly 提交于 2019-12-11 00:51:10
问题 I have a mysql query that orders by a column. It works fine if I just run the php. After I use json_encode and send it to the client, the order is changed to the primary key. Why does it do this and is there a solution? Query looks like: try{ $dbh = new PDO('mysql:host=localhost;dbname=Batik', 'root', 'root'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $_SESSION['table'] = $_SESSION['category'] = $table = $_GET['button']; $count = $dbh->query("SELECT * FROM $table ORDER BY

Join MYSQL tables and sort by two fields?

两盒软妹~` 提交于 2019-12-11 00:45:20
问题 I'm having a problem sorting my tables in MYSQL. I have my tables setup like this: Order_details Order_ID shipping_cost printed Product_details ID Order_ID SKU_location I want to SELECT all orders WHERE printed = FALSE but also sort by the shipping costs and THEN by the SKU_location How can I join the tables into one query so it sorts by shipping_cost and SKU_location where printed = false? 回答1: You can do an implicit JOIN in the following way: SELECT * FROM Order_details od JOIN Product

How can I correctly order by this varchar field representing a multiple level order using SQL?

谁说我不能喝 提交于 2019-12-11 00:38:55
问题 I am not so into DB and I am finding the following problem working on a query on a SQL Server DB of an old legacy application. I state that unfortunately I can't change the database structure\fields type (that are pretty ugly) I have the following situation: SELECT [Sottocategoria] ,[IdSottocategoria] ,[IdCategoria] ,[Note] FROM [dbo].[PROT_TITOLARIO] where IdCategoria = '5' order by IdSottocategoria and I am obtaining something like this: I have highlighted my output error. As you can see I

Creating an Order Column for encrypted data

北城余情 提交于 2019-12-11 00:35:52
问题 I am saving encrypted data to a database. Is there a way I can create a "hashcode" or fingerprint or checksum of the plain text data, that if I sort / order by on the "hashcode" the order would be the same as if I had saved the plain text data and perform the same sort / order by operation on it? I basically need a SOUNDEX() type function that will give me a value that will maintain the order of the plain text data. I would then save both encrypted data and the "hashcode" and when querying

Does table1 UNION ALL table2 guarantee output order table1, table2?

匆匆过客 提交于 2019-12-11 00:27:50
问题 SELECT a FROM b UNION ALL SELECT a FROM c UNION ALL SELECT a FROM d Does UNION ALL guarantee to print out records from tables b, c, d in that order? I.e., no records from c before any from b. This question is not for a specific DBMS. 回答1: No order by, no order guarantee whatsoever - that's for every database. And for standard SQL, an ORDER BY is applied to the results from all the unioned queries. 回答2: To be sure in order use Select 1 as TableNo,* from a union all select 2 as TableNo,* from b

SQL Server : slow query with 3 “OR” condition and order by Id

筅森魡賤 提交于 2019-12-11 00:24:12
问题 My query: SELECT TOP 30 a.* FROM Car WHERE (ModelId = 642 AND RegionId IN (23, 63, 1, 30, 5, 6, 7, 9, 15)) OR (ModelId = 242 AND RegionId IN (23, 63, 1, 30, 5, 6, 7, 9, 15, 26)) OR (ModelId = 334 AND RegionId IN (23, 63, 1, 30, 5, 6, 7, 9, 15, 26)) ORDER BY Id DESC It executes in 30 seconds. If I remove order by Id section, it executes in 1 second. Or if I remove one OR condition, it also executes in 1 second. WHERE (ModelId = 642 AND RegionId IN (23, 63, 1, 30, 5, 6, 7, 9, 15)) OR (ModelId =