sql-order-by

Best way to change order of rows in MySQL table?

99封情书 提交于 2019-12-13 07:36:45
问题 I have a MySQL table with rows that need to be sorted in a particular order decided by the user. In other words, need the user to be able to insert a new row at a random point in the table, and also move existing rows to a new random location/row number in the table output on a select query... Solutions I have thought of so far: I could use an integer with an ORDER BY, however, then i will need to update every row after the point of "insertion". I could use a timestamp and an index integer,

How to mix ASC and RANDOM() on SQLite ORDER BY

冷暖自知 提交于 2019-12-13 07:35:05
问题 So, I'm facing an error while trying to use ORDER BY for 2 columns: ... ORDER BY val, random(); Error is: "2nd ORDER BY term does not match any column in the result set" I tried different ways to combine ASC sorting for 1st column and random sorting for second column, no luck. UPDATED to provide more info CREATE TABLE tabela ( id INTEGER, val TEXT, PRIMARY KEY( id ) ); INSERT INTO tabela (val) VALUES ('paid'); INSERT INTO tabela (val) VALUES ('paid'); INSERT INTO tabela (val) VALUES ('paid');

Sorting always returns the same result

落花浮王杯 提交于 2019-12-13 07:13:46
问题 I have the following query Create procedure usp_GetBills @PageNo INT = 1, @PageSize INT = 10, @SortOrder INT = 1, @SortColumn VARCHAR(20) = '' AS BEGIN DECLARE @lSortColumn VARCHAR(20), @lFirstRec INT, @lLastRec INT SET @SortColumn = LTRIM(RTRIM(@SortColumn)) SET @lFirstRec = (@PageNo - 1) *@PageSize SET @lLastRec = (@PageNo * @PageSize + 1) ;WITH CTE_Results AS( SELECT ROW_NUMBER() OVER (ORDER BY( @SortColumn)) AS ROWNUM,P.BillNo, P.PropertyNo, P.BillDate, P.BillFromDate, P.BillToDate, P

RAND() Query and Performance

你说的曾经没有我的故事 提交于 2019-12-13 06:43:38
问题 I'm trying to prepare a query for performance. I'm hoping to remove the RAND() from the query below and replace it with a better performing alternative. Does anybody have any suggestions? SELECT video.*, video.wins / video.loses AS win_loss_ratio FROM video WHERE video.videoid NOT IN (SELECT vidlog.videoid FROM video AS vid, video_log AS vidlog WHERE vid.videoid = vidlog.videoid) AND video.round = 0 ORDER BY RAND(), win_loss_ratio DESC LIMIT 0, 2 Thanks! 回答1: Instead of using the RAND() use a

Codeigniter; Active Records Class

痴心易碎 提交于 2019-12-13 05:59:52
问题 Hello my problems is that I only want to fetch the last topic for the 'last_...'. A simple order_by won't work seeing as it will sort the forums as well, which I don't want. The code I have so far. I am doing this in CodeIgniter's built in Active Records. return $this->db->select('forums.*,') ->select('Count(topics.id) threads, Count(replies.id) replies') ->select('topics.url last_post_url, topics.name last_post_name, topics.created last_post_date') ->select('users.url user_url, users.name

Why outer order by does not work correctly?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 05:59:41
问题 I have a query like this: SELECT @rank := @rank + 3 `rank`, id, subject, name FROM quran, (select @rank := -2) q WHERE MATCH (subject, name) AGAINST ('anything') and aye IN ("10") UNION DISTINCT SELECT @rank1 := @rank1 + 3 `rank`, id, subject, name FROM quran, (select @rank1 := -1) q WHERE MATCH (subject, name) AGAINST ('anything') UNION ALL SELECT @rank2 := @rank2 + 3 `rank`, id, subject, byA FROM hadith, (select @rank2 := 0) q WHERE MATCH (subject) AGAINST ('anything') ORDER BY rank LIMIT 0

Sorting by some column and also by rand() in MySQL

爱⌒轻易说出口 提交于 2019-12-13 03:52:36
问题 Is it possible to sort a result set by some column and also by RAND()? For example: SELECT `a`, `b`, `c` FROM `table` ORDER BY `a` DESC, RAND() LIMIT 0, 10 Thank you. 回答1: What you are doing is valid - it will order the results in descending order by a but randomize the order of ties. However to do what you want you need to first use a subquery to get the latest 100 records and then afterwards sort the results of that subquery randomly using an outer query: SELECT * FROM ( SELECT * FROM

MySQL group_concat() ordering by case statement values

a 夏天 提交于 2019-12-13 02:34:51
问题 In a MySQL group_concat() clause, I'm trying to order the resulting values of a case statement. The following query configuration properly orders things.name but does not order the 'Non-US' or 'Unknown' values within the same context. SELECT things.id ,group_concat(DISTINCT CASE WHEN things.name <> 'United States' THEN 'Non-US' WHEN things.name IS NULL THEN 'Unknown' ELSE things.name END ORDER BY name SEPARATOR ', ') FROM things GROUP BY things.id I want to do something like this, but it's

MySql order by (varchar) date in Mmm-dd-yyyy

萝らか妹 提交于 2019-12-13 02:24:01
问题 So unfortunately I have a VARCHAR column of dates formatted like so: Jun-13-2013 (I understand that this is done incorrectly and should be stored in a DATETIME column in the standard format) I'm trying to grab all the data in every row and ORDER by that VARCHAR column as a DATETIME, but I'm not finding anything out there to convert my poorly formatted date column for sorting. here is my query: $result = mysql_query("SELECT * FROM archive ORDER BY crapdates DESC"); But I'm trying to convert

Ordering by COUNT() in SQL

社会主义新天地 提交于 2019-12-13 02:14:02
问题 Let's say I have a database table like this: users ------ id email referrerID How could I sort by the members with the most referrals? I was trying something along the lines of: SELECT id,email FROM users WHERE 1 ORDER BY COUNT(referrerID) DESC; But this does not seem to work. What is wrong? I think that the default value 0 may also be affecting this somehow? 回答1: Following clarification SELECT referrerID, COUNT(id) as Num FROM users GROUP BY referrerID ORDER BY CASE WHEN referrerID = 0 THEN