php/mysql multiple order by
mysql order by two columns combined
mysql order by not working
mysql order by alphabetical
order by field
2 order by php
mysql order 2
mysql secondary sort
I am working on a results table at the minute, I am wanting to sort a table, by points (highest at the top), if points are equal I want to sort by goal difference, and then if goal difference is equal I want to sort by goals scored.
So a table may look like this,
+--------+--------+----------+-----------------+--------+ | Team | Scored | Conceded | Goal Difference | Points | +--------+--------+----------+-----------------+--------+ | Team A | 20 | 10 | +10 | 15 | | Team B | 20 | 15 | +5 | 15 | | Team C | 10 | 10 | 0 | 9 | | Team D | 5 | 5 | 0 | 9 | +--------+--------+----------+-----------------+--------+
So Team A wins the league because it has a better goal difference than Team B, Team C finish above Team D because they score more goals, and all other things are equal.
Is it possible to order this way in mysql, or will I need to parse the results with PHP?
Guess what, you can pass multiple column names to ORDER BY
SELECT * FROM mytable ORDER BY Points DESC, `Goal Difference` DESC, Scored DESC
You haven't given your table structure, but as pointed out by jpg if these fields are not numeric, ORDER by field_name + 0 may be more appropriate
How to use Order by with Multiple columns in MySQL, How to use Order by with Multiple columns in MySQL This sorts your MySQL table result in Ascending or Descending order according to the specified column. Previous Post Get checked Checkboxes value with PHP. Select and Order Data From a MySQL Database. The ORDER BY clause is used to sort the result-set in ascending or descending order. The ORDER BY clause sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword. SELECT column_name (s) FROM table_name ORDER BY column_name (s) ASC|DESC.
You can do like this using query
SELECT * from `your_table` ORDER BY points DESC, goal_difference DESC, scored DESC
MySQL ORDER BY, If you want to sort the result set by multiple columns, you specify a comma-separated list of columns in the ORDER BY clause: ORDER BY column1, column2;. It is To select multiple columns, add them separated by a comma: SELECT column1, column2, column3 FROM table_name To sellect all columns, just put an asterisk (*): SELECT * FROM table_name To send the SQL statement to the MySQL database and retrieve the data returned, use the query() method of the mysqli object.
You can order by multiple columns at the same time.
SELECT some_cols FROM table WHERE (some conditions) ORDER BY col1 DESC, col2, col3;
PHP MySQL Select Data With ORDER BY Clause, Select and Order Data From a MySQL Database. The ORDER BY clause is used to sort the result-set in ascending or descending order. The ORDER BY clause PHP MySQL ORDER BY Clause In this tutorial you will learn how to sort and display the data from a MySQL table in ascending or descending order using PHP. Ordering the Result Set The ORDER BY clause can be used in conjugation with the SELECT statement to see the data from a table ordered by a specific field.
SQL ORDER BY clause, The ORDER BY clause orders or sorts the result of a query according to the NET Database SQL(2003 standard of ANSI) MySQL PostgreSQL The SQL ORDER BY clause is used with the SQL SELECT statement. You can use the ORDER BY clause to sort the result of a query on multiple columns in Using with multiple columns. Define your multiple column names in ORDER BY clause separated by a comma (,). You can also specify your sorting order ASC or DESC. Example. SELECT * FROM emp_salary ORDER BY age ASC, salary DESC. In the above query, I am ordering the emp_salary table by age in Ascending order and salary by descending order. Output
SQLite Order By, This tutorial shows you how to use SQLite ORDER BY clause to sort the result set using a single column, multiple columns in ascending and descending order. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more PHP MYSQL QUERY ORDER BY desc problem
Sorting Results with MySQL ORDER BY Clause in PHP, In this tutorial you will learn how to sort and display the data from a MySQL table in ascending or descending order using PHP. Ordering the Result Set. The C) Using MySQL ORDER BY to sort a result set by an expression example See the following orderdetails table from the sample database . The following query selects the order line items from the orderdetails table.
- No, you don't need PHP. There's more SQL than
SELECT * FROM tablename.
- Possible duplicate of Order by more than one column with mysql
- You've forgot about Points ;)
- In case that
Goal Differencemay be
`Goal Difference` + 0will be better. :-D
- Agreed @JPG but you know how these fastest gun of the west answers are shoot first edit later :) but there were a couple of others that needed shooting :)