我在 mysql 中有表"cumul_sum"我想根据条件划分"累积"列,即如果此列中的值为>= 70,那么这些值应存储在一个名为"其他"的新列中,并应将相应的sku_id存储在其前面,这是 mytable 的结构
sku_id cumulative
1 10
20 20
13 30
24 50
9 80
4 100
这就是我所期待的
sku_id cumulative sku_id_1 others
1 10 9 80
20 20 4 100
13 30
24 50
Is this any way possible
如果是,请帮忙提前致谢
这看起来不像是 sql 查询的工作,相反,您应该在 UI 中处理该格式。
因此,只需创建两个查询。
SELECT sku_id, comulative
FROM YourTable
WHERE comulative < 70
SELECT sku_id, comulative as others
FROM YourTable
WHERE comulative >= 70
如果你在PHP或类似的东西上显示(这不是真正的代码)
$size1 = query1.length;
$size2 = query2.length;
if ($size1`> $size2)
$total = $size1
else
$total = $size2
<table>
for ($i = 1 to $total)
{
<tr>
<td> if ($i <= $size1) echo $query1[$i]; </td>
<td> if ($i <= $size2) echo $query2[$i]; </td>
</tr>
}
</table>
我不确定您要实现的目标是否高效且可扩展,但这可能会完成工作。
SET @row = 0;
SET @row2 = 0;
SELECT (@row:=@row+1) AS row,
sku_id,
cumulative,
s.sku_id_1 as 'sku_id_1',
s.others as 'others'
FROM `cumul_sum`
LEFT JOIN (SELECT (@row2:=@row2+1) AS 'row',
sku_id AS 'sku_id_1',
cumulative as 'others'
FROM `cumul_sum`
WHERE `others` >= 70) s ON `cumul_sum`.`row` = `s`.`row`
WHERE `cumul_sum`.`cumulative` < 70;