sql-order-by

MySQL row number

好久不见. 提交于 2019-12-06 00:04:32
Say I have a MySQL query, for example: SELECT id, name, surname FROM employees ORDER BY id The result woud be: id name surname 1 Peter Smith 2 John Banjo ... 1384 Will Levenstein While this is an ordered query, I can always assume (as long as I don't change the table) that John Banjo will come out second. Now what if my query was SELECT id, name, surname FROM employees WHERE name = 'John' AND surname = 'Banjo' Could I somehow get what the row number would be in the first query? I'm trying to do this in a much more complicated, but always ordered query, is there any way to archieve this? SELECT

MySQL order by rand() grouped by day

╄→гoц情女王★ 提交于 2019-12-05 23:20:11
Is it possible to get random items inside the same day? For example: +----+---------------------+ | id | md | +----+---------------------+ | 1 | 2010-06-27 11:26:01 | | 2 | 2010-06-27 11:28:20 | | 3 | 2010-06-27 11:29:46 | | 4 | 2010-06-27 11:30:50 | | 5 | 2010-06-27 12:20:56 | | 6 | 2010-06-27 12:27:42 | | 7 | 2010-06-27 15:14:05 | | 8 | 2010-07-06 01:53:33 | | 9 | 2010-07-06 01:52:52 | +----+---------------------+ I want to pick random items inside the same day, but at same time i want it ordered by date desc. Something like this: +----+---------------------+ | id | md | +----+--------------

MySQL Custom Order

帅比萌擦擦* 提交于 2019-12-05 21:50:33
I have a table that I select data from with a column called parent that's of unsigned integer type. It has numbers from 0 to 12. I want to select * from table order by parent asc, but with one exception: place the 0 at the end of the select so it would be like 1,2,3,4,5,6,7,8,9,0 . Is this possible with a single select in MySQL please? I would do something like this: select * from your_table order by (parent != 0) desc, parent asc; select * from table order by case when parent is 0 then 1 else 0 end, parent asc 来源: https://stackoverflow.com/questions/3601258/mysql-custom-order

php Mysql Grouping and Ordering user messages together

隐身守侯 提交于 2019-12-05 21:29:57
PHP Mysql Table:Messages id sender receiver time_sent Message SendDel RecDel 1 1 3 2011-08-17 14:00:00 [text] 0 0 2 3 1 2011-08-17 15:00:00 [text] 0 0 3 2 4 2011-08-18 14:19:28 [text] 1 0 4 4 2 2011-08-18 15:19:28 [text] 0 0 Objective is to retrieve the highest value message(MAX) and group the messages based on sender,receiver. So message id's 1 and 2 would group together and id's 3 and 4 would group together. Example: logged in userid = 2 Should return id sender receiver time_sent Message SendDel RecDel 4 4 2 2011-08-18 15:19:28 [text] 0 0 Im not sure why but my query does not group all the

SQLAlchemy ORDER BY FIELD()

。_饼干妹妹 提交于 2019-12-05 21:29:10
问题 I am trying to sort an SQLAlchemy ORM object by a field, but with a specific order of the values (which is neither ascending or descending). If I was doing this query on MySQL, it would look like; SELECT letter FROM alphabet_table WHERE letter in ('g','a','c','k') ORDER BY FIELDS( letter, 'g','a','c','k'); for the output: letter ------ g a c k For SQLAlchemy, I've been trying things along the lines of: session.query(AlphabetTable).filter(AlphabetTable.letter.in_(('g','a','c','k'))).order_by

Negative limit offset in mysql

喜你入骨 提交于 2019-12-05 20:06:18
I'm creating a high score server and one of the needed features is being able to retrieve high scores around the users current score. I currently have the following: SELECT * FROM highscores WHERE score >= ( SELECT score FROM highscores WHERE userID = someID ) ORDER BY score, updated ASC LIMIT -9, 19 The only problem here is that the offset parameter of LIMIT can't be negative, otherwise I believe this would work dandy. So in conclusion, is there any trick / way to supply a negative offset to the LIMIT offset, or is there perhaps a better way to about this entirely? You can either do a real

Mongoid, how to order_by through a references_one association (and subsequent associations)?

笑着哭i 提交于 2019-12-05 19:34:49
Simple model: class hat embedded_in :owner field :color end class owner embedds_one :hat referenced_in :house field :name end class house references_one :owner field :number end So simply puts, we have collection of houses that are associated to an owner, and the owner can have a colored hat. I can simply sort the house by their number: House.all.order_by( [[ :number, :asc ]]) But what I want is ordering the house, by the name of their owner, ideally I would like to write: House.all.order_by( [[ :'owner.name', :asc ]]) But it does not work... And even further I would like to be able to sort

Sort certain values to the top

怎甘沉沦 提交于 2019-12-05 19:02:49
问题 I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`, `name`, `country`) VALUES (1, 'Foo', 'us'), (2, 'Bar', 'jp'), (3, 'Baz', 'us'), (4, 'Foo2', 'se'), (5, 'Baz2', 'jp'), (6, 'Bar3', 'jp'); Now, I want to be able to get a paginated list of stores that begins with the customers country. For example, an american customer would see the following list: Foo Baz Bar Foo2 Baz2 Bar3 The naive solution I'm using right now (example with an american customer and

Laravel ordering results by specific values

£可爱£侵袭症+ 提交于 2019-12-05 17:22:16
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 code to achieve these results? You need to use orderByRaw : instead of orderBy part you should use

Selecting the first row from each group, with ORDER BY more than one column

笑着哭i 提交于 2019-12-05 17:19:57
I have a table T with columns x , y , a , b such that SELECT x,y,a,b FROM T ORDER BY x,y,a,b gives me the following table x | y | a | b x1 | y1 | a1 | b1 x1 | y1 | a1 | b2 x1 | y1 | a2 | b1 x1 | y2 | a1 | b1 x1 | y2 | a1 | b2 x1 | y2 | a2 | b1 How would I get the first row of each x,y group? That is, how would I get the following table x | y | a | b x1 | y1 | a1 | b1 x1 | y2 | a1 | b1 Here is a second example: For a table T such that x | y | a | b x1 | y1 | a1 | b3 x1 | y1 | a1 | b4 x1 | y1 | a2 | b1 x1 | y1 | a2 | b2 x1 | y2 | a1 | b3 x1 | y2 | a1 | b4 x1 | y2 | a2 | b1 x1 | y2 | a2 | b2 I am