sql-order-by

mysql get table column names in alphabetical order

微笑、不失礼 提交于 2019-11-28 07:25:21
Is it possible to query a MySQL database to get the column names of a table in alphabetical order? I know that SHOW COLUMNS `table_name`; or DESCRIBE `table_name`; will give me a list of the columns in a table (along with other info), but is it possible to alter the query in order to get the columns sorted alphabetically. Adding ORDER BY 'Field' didn't work, it gave a syntax error. The ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS) provide more flexibility in MySQL: SELECT c.column_name FROM INFORMATION_SCHEMA.COLUMNS c WHERE c.table_name = 'tbl_name' -- AND c.table

SQL ORDER chars numerically

霸气de小男生 提交于 2019-11-28 07:11:55
I have a column of numbers stored as chars. When I do a ORDER BY for this column I get the following: 100 131 200 21 30 31000 etc. How can I order these chars numerically? Do I need to convert something or is there already an SQL command or function for this? Thank You. Try this: ORDER BY CAST(thecolumn AS int) This Worked for me: ORDER BY ABS(column_name) This is an issue with ordering numeric strings in a "natural sort" (if you lookup "natural sorting" on Google you'll find tons of stuff). Essentially casting the string as int and sorting on the resulting value should fix it. John The reason

How to order by count in Doctrine 2?

元气小坏坏 提交于 2019-11-28 06:58:20
I'm trying to group my entity by a field (year) and do a count of it. Code: public function countYear() { $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('b.year, COUNT(b.id)') ->from('\My\Entity\Album', 'b') ->where('b.year IS NOT NULL') ->addOrderBy('sclr1', 'DESC') ->addGroupBy('b.year'); $query = $qb->getQuery(); die($query->getSQL()); $result = $query->execute(); //die(print_r($result)); return $result; } I can't seem to say COUNT(b.id) AS count as it gives an error, and I do not know what to use as the addOrderby(???, 'DESC') value? what is the error you get when using

mysql update a column with an int based on order

自作多情 提交于 2019-11-28 06:37:18
Lets say I have these columns uniqueID|Money|Quantity|MoneyOrder|QuantityOrder 1|23|12|| 2|11|9|| 3|99|100|| What I want to do is update MoneyOrder and QuantityOrder based on the value of ORDER BY . So the results would be: uniqueID|Money|Quantity|MoneyOrder|QuantityOrder 1|23|12|2|1 2|11|90|1|2 3|99|100|3|3 I want the update to operate like an identity column without actually making it an identity column. I know that I could just order by 'x' and the order would be the result but I want to generate a report where you can see the item line by line. Is something like this possible update

Alphanumeric Order By in Mysql

痴心易碎 提交于 2019-11-28 06:02:32
问题 How can i make only numeric order by when the column containing alphanumeric characters in mysql ? column (name) is unique field. my table contains the records, id name 1 ab001 2 ab010 3 aa002 4 ac004 5 ba015 6 ba006 7 aa005 8 ac003 The results must be like this, id name 1 ab001 3 aa002 8 ac003 4 ac004 7 aa005 6 ba006 2 ab010 5 ba015 When I am trying this query Select * from test order by name , I am getting the results order by alpha characters only. How do I get this ? 回答1: Assuming your

java comparator, how to sort by integer?

一世执手 提交于 2019-11-28 05:46:06
Im trying to learn comparator in java and I have found this great example online, my question is how would this code be changed so that the pet names are ordered by age and in descending order so that the oldest is first and youngest is last? class Dog implements Comparator<Dog>, Comparable<Dog>{ private String name; private int age; Dog(){ } Dog(String n, int a){ name = n; age = a; } public String getDogName(){ return name; } public int getDogAge(){ return age; } // Overriding the compareTo method public int compareTo(Dog d){ return (this.name).compareTo(d.name); } // Overriding the compare

MySQL: Order by field size/length

本秂侑毒 提交于 2019-11-28 05:10:00
Here is a table structure (e.g. test): __________________________________________ | Field Name | Data Type | |________________|_________________________| | id | BIGINT (20) | |________________|_________________________| | title | varchar(25) | |________________|_________________________| | description | text | |________________|_________________________| A query like: SELECT * FROM TEST ORDER BY description DESC; But I would like to order by the field size/length of the field description. The field type will be TEXT or BLOB. João Silva SELECT * FROM TEST ORDER BY LENGTH(description) DESC; The

MySQL sort order by array value

故事扮演 提交于 2019-11-28 04:56:46
I need to run a MySQL query where the order is determined by an array value. My array is variable but the values in the array correspond to a field in my DB table called 'ID' so I want the result to be returned in the ID order 9, 1, 4. Array ( [0] => 9 [1] => 1 [2] => 4 ) Is this possible in MySQL or would it be possible to sort the MySQL $result using the array after? You can assume the only values being returned are those in the array. ORDER BY field(id, 9, 1, 4); http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_field hari jee You want to get a list of items with the ids

avoid Sorting by the MYSQL IN Keyword

牧云@^-^@ 提交于 2019-11-28 04:47:04
问题 When querying the db for a set of ids, mysql doesnot provide the results in the order by which the ids were specified. The query i am using is the following: SELECT id ,title, date FROM Table WHERE id in (7,1,5,9,3) in return the result provided is in the order 1,3,5,7,9. How can i avoid this auto sorting 回答1: If you want to order your result by id in the order specified in the in clause you can make use of FIND_IN_SET as: SELECT id ,title, date FROM Table WHERE id in (7,1,5,9,3) ORDER BY

Is it possible to refer to column names via bind variables in Oracle?

試著忘記壹切 提交于 2019-11-28 04:38:52
问题 I am trying to refer to a column name to order a query in an application communicating with an Oracle database. I want to use a bind variable so that I can dynamically change what to order the query by. The problem that I am having is that the database seems to be ignoring the order by column. Does anyone know if there is a particular way to refer to a database column via a bind variable or if it is even possible? e.g my query is SELECT * FROM PERSON ORDER BY :1 (where :1 will be bound to