简体   繁体   中英

How would you order by two columns in MYSQL Database?

I have a complex problem that I have been working on for a while. I don't even know if this is possible to do.This is an iOS app where users can reorder rows in the tableview and it updates the database (ordering02) with the temporary indexpath.row.


Columns to order by:

  • ordering01: Products are ordered 1..2..3... and so on
  • ordering02: When product is moved to a new position (indexPath.row) in the app this temporarily holds the new position in the mysql database until the user saves changes. Once the user saves changes that position is then saved into ordering01.

What I am trying to do is display the unsaved product order in the app (tableView). To do this I will need to sort by both ordering01 & ordering02. The complicated part is that I need the query to check if ordering02 exists. If ordering02 exists then that's the column that is used but if it doesn't exist then the column used is ordering01.


$sql = mysql_query("SELECT * FROM products WHERE status = '1' ORDER BY [Check if ordering 02 exists, if it does use ordering 02 if not use ordering01] "); 

while($row = mysql_fetch_array($sql01)){ 

}

Does this Make sense and is it even possible?

For the temporary storage, why not use arrays, until the user clicks the save, then take from the array and insert into your table.

The best way to merge the two tables, is using the "JOINS" command in your SQL code and checking if the table exists, PHP offers a call method called "exists()", so you would first call the name of the table (ordering02) inside your if statement.

So you get:

$con = mysqli_connect(...);
$tblName = "ordering02";

If (exists($tblName)){
    # Perform action
}else{
    # table doesn't exist
}

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