这是我的桌子。我想在CR和DR之后更新NewBalance。
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
/*----------------------------------------------------------------------
Adding Ajax Search for POSTS
-------------------------------------------------------------------------*/
function ex_get_posts_ajax_callback(){
global $post;
// we will pass post IDs and titles to this array
$return = array();
// you can use WP_Query, query_posts() or get_posts() here - it doesn't matter
$search_results = new WP_Query( array(
's'=> $_GET['q'], // the search query
'post_type' => 'post',//post type
'post_status' => 'publish', // if you don't want drafts to be returned
'ignore_sticky_posts' => 1,
'posts_per_page' => 20 // how much to show at once
) );
if( $search_results->have_posts() ) :
while( $search_results->have_posts() ) : $search_results->the_post();
// shorten the title a little
$title = ( mb_strlen( $search_results->post->post_title ) > 50 ) ? mb_substr( $search_results->post->post_title, 0, 49 ) . '...' : $search_results->post->post_title;
$return[] = array( $search_results->post->ID, $title ); // array( Post ID, Post Title )
endwhile;
endif;
wp_send_json( $return );
}
add_action( 'wp_ajax_getpostsearch', 'ex_get_posts_ajax_callback' ); // wp_ajax_{action}
//html output for edit field
function ex_product_cat_feature_posts($post_object) {
global $post;
// Nonce field to validate form request came from current site
wp_nonce_field( basename( __FILE__ ), '_feature_post_nonce' );
$html = '';
// always array because we have added [] to our <select> name attribute
$ex_product_cat_feature_post = get_post_meta( $post_object->ID, 'ex_product_cat_feature_post', true );
$html .= '<tr class="form-field">';
$html .= '<th scope="row" valign="top"><label for="catshort_button_type">Select Feature post:</label></th>';
$html .= '<td>';
$html .= '<select id="ex_product_cat_feature_post" name="ex_product_cat_feature_post[]" multiple="multiple" style="width:99%;max-width:25em;">';
if( $ex_product_cat_feature_post ) {
foreach( $ex_product_cat_feature_post as $post_id ) {
$title = get_the_title( $post_id );
// if the post title is too long, truncate it and add "..." at the end
$title = ( mb_strlen( $title ) > 50 ) ? mb_substr( $title, 0, 49 ) . '...' : $title;
$html .= '<option value="' . $post_id . '" selected="selected">' . $title . '</option>';
}
}
$html .= '</select>';
$html .= '</td>';
$html .= '</tr>';
echo $html;
//==========================================
?>
<script>
(function ($) {
'use strict';
$(function () {
//--------------------------------------------------------------------------
// multiple select with AJAX search
$('#ex_product_cat_feature_post').select2({
ajax: {
url: ajaxurl, // AJAX URL is predefined in WordPress admin
dataType: 'json',
delay: 250, // delay in ms while typing when to perform a AJAX search
data: function (params) {
return {
q: params.term, // search query
action: 'getpostsearch' // AJAX action for admin-ajax.php
};
},
processResults: function( data ) {
var options = [];
if ( data ) {
// data is the array of arrays, and each of them contains ID and the Label of the option
$.each( data, function( index, text ) { // do not forget that "index" is just auto incremented value
options.push( { id: text[0], text: text[1] } );
});
}
return {
results: options
};
},
cache: true
},
minimumInputLength: 3 // the minimum of symbols to input before perform a search
});
//----------------------------------------------------------------------------------------
});
})(jQuery);
</script>
<style type="text/css">
iframe#description_ifr{
min-height:220px !important;
}
</style>
<?php
}
add_action('product_cat_add_form_fields', 'ex_product_cat_feature_posts', 10, 1);
add_action('product_cat_edit_form_fields', 'ex_product_cat_feature_posts', 10, 1);
// Save extra taxonomy fields callback function.
function ex_product_cat_feature_posts_save($term_id) {
$ex_product_cat_feature_post = filter_input(INPUT_POST, 'ex_product_cat_feature_post');
update_term_meta($term_id, 'ex_product_cat_feature_post', $ex_product_cat_feature_post);
}
add_action('edited_product_cat', 'ex_product_cat_feature_posts_save', 10, 1);
add_action('create_product_cat', 'ex_product_cat_feature_posts_save', 10, 1);
id amount trs_typ NewBalance
1 10 DR 500
2 11 DR 563
3 25 CR 256
4 65 DR 354
5 58 CR 658
6 45 CR 542
7 65 DR 322
UPDATE test
SET Total=(CASE
WHEN transection_type = 'CR' THEN total+amount
WHEN transection_type='DR' THEN Total-amount
END)
从底部
322 + 45 = 367
答案 0 :(得分:0)
这似乎是ID倒序的运行总计。您可以使用变量来存储运行余额来实现此目的。
drop table if exists t;
create table t
(id int,amount int, trs_typ varchar(2), NewBalance int);
insert into t values
(1 , 10 , 'DR' , 500),
(2 , 11 , 'DR' , 563),
(3 , 25 , 'CR' , 256),
(4 , 65 , 'DR' , 354),
(5 , 58 , 'CR' , 658),
(6 , 45 , 'CR' , 542),
(7 , 65 , 'DR' , 322);
update t set newbalance = 0 where id <> (select maxid from (select max(id) maxid from t) s);
update t join(
select t.*,
if(newbalance > 0 ,@rb:=newbalance,if(trs_typ = 'cr',@rb:=@rb+amount,@rb:=@rb - amount)) rb
from t
cross join(select @rb:=0) rb
order by id desc
) t1 on t1.id = t.id
set t.newbalance = t1.rb
;
select * from t;
+------+--------+---------+------------+
| id | amount | trs_typ | NewBalance |
+------+--------+---------+------------+
| 1 | 10 | DR | 364 |
| 2 | 11 | DR | 374 |
| 3 | 25 | CR | 385 |
| 4 | 65 | DR | 360 |
| 5 | 58 | CR | 425 |
| 6 | 45 | CR | 367 |
| 7 | 65 | DR | 322 |
+------+--------+---------+------------+
7 rows in set (0.00 sec)
请注意,ID 1和ID 2的newbalance与您的预期输出不同(我认为正确)