简体   繁体   中英

ordering two columns php mysql


I have a query:

<?php

$results = $dbConn->select("SELECT entryA, entryB FROM table");

/**
Displays the rows on $results (entryA, entryB)
1      7
8      5
4      3
5      8
7      1
3      4
**/

$results = $dbConn->select("SELECT entryA, entryB FROM table ORDER BY ?");
/**
The correct output must be: (entryA, entryB)
1      7
7      1
8      5
5      8
4      3
**/


?>


How can i possible to order two columns that equal/match to each other ids in a different row?

Thanks in advance.

Yes, it is. For example

SELECT entryA, entryB FROM table ORDER BY entryA*entryA+entryB*entryB

-in my samples I'm supposing you have both pair variants, i,e {1,7} and {7,1} , for example.

This will group same pairs independent of elements order, but you may wish to have additional order condition - then simply add it to ORDER BY clause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM