在下面看一下
+----+---------+---------+----+---------+---------+
| id | name | order | one| two | three |
+----+---------+---------+----+---------+---------+
| 1 | stan | 1 | 5 | suger | true |
| 2 | kyle | 1 | 4 | coffe | yes |
| 3 | stan | 2 | 3 | banana | no |
| 4 | kyle | 2 | 4 | orange | false|
+----+---------+---------+----+---------+---------+
从上方看,我有两个斯坦和两个凯尔。现在发生的事情是,如果我添加一个notstan,我需要代码来查看顺序中的最大数量,然后在该订单字段中添加+1。
简而言之,在mysql表中添加一行,按名称将顺序按一个+1组递增。如何在PHP和Mysql中实现呢?还是我认为只能在Mysql Query中处理?因此,如果我需要添加以下信息,
5斯坦(计算顺序)3香蕉为真。
答案 0 :(得分:2)
尽管按名称分组不是一个好主意,因为多个人可以具有相同的名称,但我认为以下是这样的解决方案:
const some_async_op = cb => setTimeout(cb, 500)
let resolver, p
function api1(cb) {
if (!p) p = new Promise(resolve => {
resolver = resolve
})
some_async_op(() => {
cb()
resolver()
})
}
function api2() {
(p || (p = new Promise(resolve => {
resolver = resolve
})))
.then(() => console.log('api 2 done'))
}
// call api2 first
api2()
// sometime later call api1
setTimeout(() => api1(() => console.log('api1 callback fired'), 600))
http://sqlfiddle.com/#!9/b25dbf/3/0
用于将值添加到表中:
select name, max(`order`)+1
from orders
group by name;
http://sqlfiddle.com/#!9/01db84/1
添加多个值:
insert into orders(name,`order`)
select name, max(`order`)+1
from orders
where name='stan'
group by name;