sql-order-by

Order by Maximum condition match

删除回忆录丶 提交于 2019-11-29 07:02:47
Please help me to create a select query which contains 10 'where' clause and the order should be like that: the results should be displayed in order of most keywords(where conditions) matched down to least matched. NOTE: all 10 condition are with "OR". Please help me to create this query. i am using ms-sql server 2005 Like: Select * from employee where empid in (1,2,4,332,434) or empname like 'raj%' or city = 'jodhpur' or salary >5000 In above query all those record which matches maximum conditions should be on top and less matching condition record should be at bottom. SELECT * FROM (SELECT

MySQL specify arbitrary order by id

跟風遠走 提交于 2019-11-29 06:36:16
Is it possible to specify an arbitrary order for a MySQL SELECT statement? E.g., SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY (1, 3, 2, 9, 7); The order of the numbers listed directly after IN do not seem to matter. The Scrum Meister FIND_IN_SET function will do the trick SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY FIND_IN_SET(id, '1,3,2,9,7'); http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set EDIT: Note the lack of spaces in the string argument of the find_in_set function. Check out mysql's ORDER BY FIELD . I think it will do

How mysql order the rows with same values

纵然是瞬间 提交于 2019-11-29 05:54:27
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? I'd advise against making that assumption. In standard SQL, anything not required by an explicit ORDER BY clause is implementation dependent. I can't speak for MySQL, but on e.g. SQL Server, the output

How to make MYSQL query results ORDER BY conditions order?

巧了我就是萌 提交于 2019-11-29 05:48:17
问题 My query string is like: SELECT ... FROM maintable LEFT JOIN table1 on (maintable.id = table1.idx) LEFT JOIN table2 on (table1.idy = table2.idy) LEFT JOIN table3 on (table2.idz = table3.idz) WHERE (condition1 OR condition2 OR condition3) AND maintable.status = static //condition1 & condition2 & condition3 are kind of table3.idz = 101, table3.idz = 3, maintable.id IN (1,2,3,4), and so on For the results I want entries that meet condition1 to be returned first, then entries that meet condition2

PostgreSQL - repeating rows from LIMIT OFFSET

十年热恋 提交于 2019-11-29 05:08:57
问题 I noticed some repeating rows in a paginated recordset. When I run this query: SELECT "students".* FROM "students" ORDER BY "students"."status" asc LIMIT 3 OFFSET 0 I get: | id | name | status | | 1 | foo | active | | 12 | alice | active | | 4 | bob | active | Next query: SELECT "students".* FROM "students" ORDER BY "students"."status" asc LIMIT 3 OFFSET 3 I get: | id | name | status | | 1 | foo | active | | 6 | cindy | active | | 2 | dylan | active | Why does "foo" appear in both queries?

How to sort by numbers first with Oracle SQL query?

左心房为你撑大大i 提交于 2019-11-29 04:32:50
I have this table with a 'title' field which is varchar2 and I want to select all rows and sort them first by number and then by the alphabet as it normally happens. For instance, I currently get this using a simple ORDER BY title in the end: Abc Def 321 But I want this: 321 Abc Def The weird thing is that SQL Developer shows the "right" order, with numbers first. But on my app (PHP using OCI8) it shows numbers last. Not an Oracle expert, but you are supposed to be able to do it without altering the session with SELECT * FROM my_data SORT by NLSSORT(title,’NLS_SORT=BINARY_AI’) where you can

MySQL Sort By 2 Columns

Deadly 提交于 2019-11-29 03:46:09
I have a table which holds information on television programs and I want to order it by Seasons and then by episodes. Here's a basic view of what I have: +---+--------+---------+ |id | Season | Episode | +---+--------+---------+ | 1 | 1 | 1 | +---+--------+---------+ | 1 | 1 | 2 | +---+--------+---------+ | 1 | 2 | 1 | +---+--------+---------+ | 1 | 2 | 3 | +---+--------+---------+ So I select what I need and order by Season. But there's going to be a lot between seasons so I need to sort episodes too, but without it affecting seasons. Stefan Gehrig Do you mean: SELECT id, Season, Episode FROM

LEFT JOIN order and limit

心不动则不痛 提交于 2019-11-29 03:40:59
This is my query: SELECT `p`.`name` AS 'postauthor', `a`.`name` AS 'authorname', `fr`.`pid`, `fp`.`post_topic` AS 'threadname', `fr`.`reason` FROM `z_forum_reports` `fr` LEFT JOIN `forums` `f` ON (`f`.`id` = `fr`.`pid`) LEFT JOIN `forums` `fp` ON (`f`.`first_post` = `fp`.`id`) LEFT JOIN `ps` `p` ON (`p`.`id` = `f`.`author_guid`) LEFT JOIN `ps` `a` ON (`a`.`account_id` = `fr`.`author`) My problem is this left join: SELECT `a`.`name`, `a`.`level` [..] LEFT JOIN `ps` `a` ON (`a`.`account_id` = `fr`.`author`) Since, in case a has MANY rows and it'll return like in my case: NAME | LEVEL Test1 | 1

How to optimize an ORDER BY for a computed column on a MASSIVE MySQL table

泪湿孤枕 提交于 2019-11-29 02:43:34
问题 I have a very large (80+ million row) de-normalized MySQL table. A simplified schema looks like: +-----------+-------------+--------------+--------------+ | ID | PARAM1 | PARAM2 | PARAM3 | +-----------+-------------+--------------+--------------+ | 1 | .04 | .87 | .78 | +-----------+-------------+--------------+--------------+ | 2 | .12 | .02 | .76 | +-----------+-------------+--------------+--------------+ | 3 | .24 | .92 | .23 | +-----------+-------------+--------------+--------------+ | 4

Django custom order_by

删除回忆录丶 提交于 2019-11-29 02:33:54
I've got a model eg. Car with a foreign key eg. Owner, which may or may not be blank. The Car has a creation_date. I would like to order these cars by date, but if the car has an owner, the date of birth of the owner must be taken instead of the creation_date of the car. Is this possible? Timmy O'Mahony Have a look at this similar question: Good ways to sort a queryset? - Django You can't use the model's Meta ordering as it only accepts one field https://docs.djangoproject.com/en/dev/ref/models/options/#ordering You can't use the query order_by('creation_date', 'birthdate') as it only sorts by