我有一个具有以下架构的表
a | b | c
qqq | www | ddd/ff
fff | ggg | xx/zz
jjj | gwq | as/we
我该如何编写查询,以便我的数据显示为
a | b | c_1 | c_2
qqq | www | ddd | ff
答案 0 :(得分:2)
add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');
function display_price_in_variation_option_name( $term ) {
global $product;
if ( empty( $term ) ) {
return $term;
}
if ( empty( $product->id ) ) {
return $term;
}
$variation_id = $product->get_children();
foreach ( $variation_id as $id ) {
$_product = new WC_Product_Variation( $id );
$variation_data = $_product->get_variation_attributes();
foreach ( $variation_data as $key => $data ) {
if ( $data == $term ) {
$html = wp_kses( woocommerce_price( $_product->get_price() ), array() );
$html .= ' - ' . $term;
$html .= ( $_product->get_stock_quantity() ) ? ' - ' . $_product->get_stock_quantity() : '';
return $html;
}
}
}
return $term;
}
或者,如果列c的格式不总是xxx / yyy,则需要验证charindex位置:
declare @t table(a varchar(20),b varchar(20),c varchar(20))
insert into @t values('qqq','www','ddd/ff')
SELECT a, b,
left(c,charindex('/',c)-1) As c_1,
right(c,charindex('/',reverse(c))-1) As c_2
FROM @t
答案 1 :(得分:1)
您可以使用以下方式:
select LEFT(name, CHARINDEX('/', name)-1) from test_table;
在斜杠前返回字符串名称的左部分,在斜杠后返回以下命令。
select RIGHT(name, CHARINDEX('/', name)-1) from test_table;
如您所见,我做了一个完整的例子:
create table test_table ( name varchar(50), substr1 varchar(50), substr2 varchar(50));
insert into test_table(name) values ('sub1/sub2');
update test_table set substr1 =
(select LEFT(name, CHARINDEX('/', name)-1) from test_table);
update test_table set substr2 =
(select RIGHT(name, CHARINDEX('/', name)-1) from test_table);
select * from test_table;
结果是:
name | substr1 | substr2
sub1/sub2 | sub1 | sub2
答案 2 :(得分:0)
也可以使用Patindex代替Charindex
SELECT a,b,LEFT(c,PATINDEX('%/%',c)-1), RIGHT(c,PATINDEX('%/%',REVERSE(c))-1) FROM @t