sql-order-by

Laravel Eloquent: Ordering results of all()

让人想犯罪 __ 提交于 2019-11-28 03:13:08
I'm stuck on a simple task. I just need to order results coming from this call $results = Project::all(); Where Project is a model. I've tried this $results = Project::all()->orderBy("name"); But it didn't work. Which is the better way to obtain all data from a table and get them ordered? You can actually do this within the query. $results = Project::orderBy('name')->get(); This will return all results with the proper order. You could still use sortBy (at the collection level) instead of orderBy (at the query level) if you still want to use all() since it returns a collection of objects.

PHP and MySQL: Order by most recent date and limit 10

故事扮演 提交于 2019-11-28 01:50:26
I am building a notes system on my site and I've got to the stage where users can post notes into the MySQL database using PHP and then PHP prints them out on a page. However, when they print/echo out, the oldest one appears first but I want the most recent first. I also want them to be limited to 10, so only 10 appear on the page. Here is my PHP code, your help will be much appreciated: // initialize some variables $notedisplaylist = ""; $myObject = ""; $result = mysql_query("SELECT * FROM notes WHERE note_author_id='$u_id' ORDER BY date_time"); while($row = mysql_fetch_array($result)){ $note

Find TOP 10 latest record for each BUYER_ID for yesterday's date

橙三吉。 提交于 2019-11-28 01:42:31
问题 This is the below table CREATE TABLE IF NOT EXISTS TestingTable1 ( BUYER_ID BIGINT, ITEM_ID BIGINT, CREATED_TIME STRING ) And this is the below data in the above table- BUYER_ID | ITEM_ID | CREATED_TIME ------------+------------------+----------------------- 1015826235 220003038067 2012-07-09 19:40:21, 1015826235 300003861266 2012-07-09 18:19:59, 1015826235 140002997245 2012-07-09 09:23:17, 1015826235 210002448035 2012-07-09 22:21:11, 1015826235 260003553381 2012-07-09 07:09:56, 1015826235

SQL: How to keep rows order with DISTINCT?

∥☆過路亽.° 提交于 2019-11-28 01:28:59
问题 The following SQL query: SELECT messages.id, messages.created_at, comments.created_at FROM messages LEFT JOIN comments ON comments.message_id = messages.id WHERE (messages.id IN (429,443)) ORDER BY GREATEST(messages.created_at, comments.created_at) DESC returns: id messages.created_at comments.created_at -------------------------------------------------------- 443 2 5 429 1 4 443 2 3 (I replaced dates with numbers for readability) To get each id only once I added DISTINCT : SELECT DISTINCT

LINQ OrderBy not ordering .. changing nothing .. why?

孤街醉人 提交于 2019-11-28 00:41:18
Any idea why the LINQ OrderBy is not working in following code, (have no errors but method does not sort ...) First my own type public class IQLinksView { public int id { get; set; } public int catid { get; set; } public int? viewed {get;set;} public string name {get;set;} public string desc {get;set;} public string url {get;set;} public string pic {get;set;} public string cat {get;set;} } then query : IQueryable<IQLinksView> newView = from links in this.emContext.tbl_otherlinks select new IQLinksView { id = links.pklinkid, catid = links.tbl_catgeory.pkcategoryid, viewed = links.linkviewed,

Properly sorting dotted numbers stored as character in SQL Server

て烟熏妆下的殇ゞ 提交于 2019-11-28 00:26:29
I have a SQL table that stores a custom item number. Each of these can have a child broken off from it with a separator of . . Each of those can have a child too. An example of what it could be (again, dynamic, don't know what it will be): Item Number 1 1.1 1.1.1 1.1.1.1 1.1.1.1.a 1.1.1.1.b 10 11 2.1 2.10 2.2 2.20 20 3 30 The thing that makes this tough is those numbers are created on the fly and not necessarily in order. You may create 5 numbers (1, 2, 3, 4, 5) and then create a child of 1 so it will not be stored in order in the db. How do I select from the table and order by the Item Number

Is it possible to use bind_param for ORDER BY? [duplicate]

本小妞迷上赌 提交于 2019-11-28 00:09:50
This question already has an answer here: Can I parameterize the table name in a prepared statement? 2 answers In my mind I have a query that goes something like this: $sort = isset($sort) ? sanitize($_sort) : 'id'; if ($result = $link->prepare(" SELECT id, price FROM items ORDER BY ? ")) { $result->bind_param("s", $sort); $result->execute(); etc... } When I run this code block without setting the sort variable it runs without an error relating to the use of the ? in the ORDER BY clause and a result set is displayed in what appears to be a result set with "ORDER BY id". If I set the sort

How mysql order the rows with same values

旧时模样 提交于 2019-11-27 23:37:50
问题 In my database I have some records where I am sorting by what happens to be the same value: | col1 | timestamp | | row1 | 2011-07-01 00:00:00 | | row2 | 2011-07-01 00:00:00 | | row3 | 2011-07-01 00:00:00 | SELECT ... ORDER BY timestamp It looks like the result is in a random order. Is the random order consistent. That means if i have these datas in two mysql servers.Can I expect the same result? 回答1: I'd advise against making that assumption. In standard SQL, anything not required by an

OrderBy and OrderByDescending are stable?

廉价感情. 提交于 2019-11-27 23:26:38
I am currently reading Pro LINQ c# 2008, and in page 87 the guy says OrderBy and OrderByDescending are stable. But he says exactly the opposite in page 96. It looks to me as he is referring to exactly the same functions, so I don't get it. Are they stable or not? Yes, they're definitely stable. I picked up the same error in my review of the book. Joe responded to that bit of my review with this: Just for your reader's knowledge, the ordering is now specified to be stable. Initially it was unstable, and was later changed to be stable but I was told it would be specified to be unstable, but

Can someone recommend a good tutorial on MySQL indexes, specifically when used in an order by clause during a join? [closed]

主宰稳场 提交于 2019-11-27 22:42:39
问题 I could try to post and explain the exact query I'm trying to run, but I'm going by the old adage of, "give a man a fish and he'll eat for a day, teach a man to fish and he'll eat for the rest of his life." SQL optimization seems to be very query-specific, and even if you could solve this one particular query for me, I'm going to have to write many more queries in the future, and I'd like to be educated on how indexes work in general. Still, here's a quick description of my current problem. I