sql-order-by

Group by user and show latest in MYSQL not working

戏子无情 提交于 2019-12-10 12:13:15
问题 I have a social network I am coding but it's a bit different than others, in this case there is only ever one status show per user on a feed section. So I need to sort the status by date with the latest ones on top but also group by the userID unfortunately I can't seem to get this working.... This is my current query: SELECT * FROM status WHERE userID != "83" #This is just so my statuses don't show GROUP BY userID ORDER BY addedDate DESC LIMIT 10 I expect to see the latest status results and

mysql sort table and get row position

我的未来我决定 提交于 2019-12-10 11:38:21
问题 I've got a problem in mysql, I have a table of ID and nubmer ... somthing like that ID -------- number 3 -------- 340 1 -------- 10 12 -------- 23 And now I would like to selecet by ID, let's say ID=3 and is there a way of saying what is the position of this row in sorted table? In this case, ID = 3 would have first position, cuase of highest value in number. ID=12 would have second position ... and so on. 回答1: set @row_number:=0; select * from (select ID, @row_number:=@row_number+1 from your

How to optimize MySQL “Order By Limit 1” in queries that join multiple tables?

心不动则不痛 提交于 2019-12-10 11:32:34
问题 So I have a query like this: SELECT tablea.name, tablea.views from tablea inner join tableb on (tablea.id = tableb.id and tablea.balance > 0) order by tablea.views asc limit 1 However, the problem is that when I run it, it runs quite slow (4+ seconds). Interestingly, when the 'order by' clause is removed, while keeping the limit 1, it runs in 0.005 seconds (approx). Even more interestingly: when I don't join it to tableb, i.e.: SELECT tablea.name, tablea.views from tablea where tablea.balance

How to sort a varchar datatype column by its numbers in SQL Server

我们两清 提交于 2019-12-10 11:17:14
问题 I have a column in SQL Server that contains data like this: 1.1.1.QuestionText 1.1.1.Question 1.1.1(a).Questions 1.1.2.Questionswithtext 1.1.2(b).Text 10.1.1.Answers 2.1.1.Questions 2.2.2.QuestionText How do I display this in ascending order? 回答1: This should work: SELECT name FROM ( SELECT name, SUBSTRING(name, 1, LEN(name) - PATINDEX('%[0-9]%', REVERSE(name)) + 1) n FROM #tmp ) v ORDER BY CAST(PARSENAME(n, 4) AS INT), CAST(PARSENAME(n, 3) AS INT), CAST(PARSENAME(n, 2) AS INT), CAST

ProgrammingError: when using order_by and distinct together in django

旧时模样 提交于 2019-12-10 11:09:55
问题 I have a model like below class ProductScore(models.Model): client = models.ForeignKey(User) created = models.DateTimeField(default=datetime.datetime.now) score = models.IntegerField() scale = models.ForeignKey(Product) As of now i am using the below query to filter out the duplicates from the above model scores = ProductScore.objects.filter(client=request.user).distinct('scale') By the above query it was returning the unique results but are old(created date was very old), i mean for example

T-Sql - Order By on Alphanumeric

社会主义新天地 提交于 2019-12-10 11:05:52
问题 i have a list of alphanumeric tokens, say '1a', '1b', '02', '03', '10', '11', etc... Now, what's the best way to do an order by on this list of tokens? I am getting '1a', '1b', '10', '11', '02', '03', but i need it to be '1a', '1b', '02', '03', '10', '11' UPDATE ok, i am doing this after the suggestion but it's not working. declare @tokens table(token varchar(20)); insert into @tokens select '1a' select '1b' select '02' select '10' select * from @tokens order by case when ISNUMERIC(token) = 1

ORDER BY Color with Hex Code as a criterio in MySQL

こ雲淡風輕ζ 提交于 2019-12-10 08:04:25
问题 I have a table that contains color options for a product. The color options include a hex color code, which is used to generate the UI (HTML). I would like to sort the rows so that the colors in the UI look like a rainbow, instead of the current order that sorts based off of the Name of the color (not very useful). Here is what my query looks like. I get the R G B decimal values from the hex code. I just don't know how to order it. I've looked into color difference algorithms. They seem more

Using DQL functions inside Doctrine 2 ORDER BY

久未见 提交于 2019-12-10 07:14:29
问题 I'm doing a project in Symfony 2.3 with Doctrine 2.4 using MySQL database. I have an Entity of FieldValue (simplified): class FieldValue { /** * The ID * * @var integer */ protected $fieldValueId; /** * Id of associated Field entity * * @var integer */ protected $fieldId; /** * Id of associated user * * @var integer */ protected $userId; /** * The value for the Field that user provided * * @var string */ protected $userValue; /** * @var \MyProjectBundle\Entity\Field */ protected $field; /** *

Sort numbers (and numbers+letters) with sqlite

ぐ巨炮叔叔 提交于 2019-12-10 06:43:02
问题 I'm trying to sort a VarChar field with Sqlite. The field can contain numbers or numbers+letters, but I need to sort in numerical order, like this: 1 1a 1b 5 5x 5y 10 10d 10e 10g1 11 11a 11b 100c 100f Any ideas? I've been able to do this... it is close (but not quite) what I need: Pad the start of the field with '00000', and then sort on the 1st five letters 回答1: Easy select col from tbl order by col*1, col 回答2: There's no easy way to do this with the built-in functions. Use sqlite3_create

Mysql query which returns category tree

冷暖自知 提交于 2019-12-10 05:02:13
问题 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)