I want to compare two prices in my database. I get a row with any id:
$price1 = $wpdb->get_row( "SELECT * FROM $wpdb->prices WHERE id = 10" );
I want to get up the row from this table means I want to get row same bellow but if row by id 9 is deleted it's missing:
$price1 = $wpdb->get_row( "SELECT * FROM $wpdb->prices WHERE id = 9" );
How do this?
I want to compare two prices in my database. I get a row with any id:
$price1 = $wpdb->get_row( "SELECT * FROM $wpdb->prices WHERE id = 10" );
I want to get up the row from this table means I want to get row same bellow but if row by id 9 is deleted it's missing:
$price1 = $wpdb->get_row( "SELECT * FROM $wpdb->prices WHERE id = 9" );
How do this?
Share Improve this question asked Mar 23, 2020 at 16:34 hossein naghnehhossein naghneh 1311 silver badge8 bronze badges 1- Was my answer helpful to you?! – WordPress Speed Commented Mar 28, 2020 at 15:59
3 Answers
Reset to default 0you should add another field to your prices
table called previous_id
and keep it updated on every insert and delete query.
This way you may always have the track for previous record. If you deletethat record, you may assign its previous_id
into the next record and if you insert new record, you may add the last available id into its previous_id
.
You can get row by id = 10
if it is deleted then checks for id = 9
and so on.
$price1 = '';
for($id = 10; $price1==''; $id--){
$price1 = $wpdb->get_row( "SELECT * FROM book WHERE id = $id" );
}
If id = 10
is present, it will be selected directly. Loop will run only once.
If you want to get next row (i.e.11th) simply replace $id--
with $id++
I found my answer. I have a field that common between these two rows and handle width limit parameter in my query.
$wpdb->prepare("SELECT * FROM $table_name WHERE post_id = %d ORDER BY id DESC LIMIT 2", $post_id);