mysql怎么按 in 里面 id 的顺序排列

mysql怎么按 in 里面 id 的顺序排列,第1张

表结构如下:

mysql>select * from test

+----+-------+

| id | name |

+----+-------+

| 1 | test1 |

| 2 | test2 |

| 3 | test3 |

| 4 | test4 |

| 5 | test5 |

+----+-------+

执行以下SQL:

mysql>select * from test where id in(3,1,5)

+----+-------+

| id | name |

+----+-------+

| 1 | test1 |

| 3 | test3 |

| 5 | test5 |

+----+-------+

3 rows in set (0.00 sec)

这个select在mysql中得结果会自动按照id升序排列,

但是我想执行"select * from test where id in(3,1,5)"的结果按照in中得条件排序,即:3,1,5,

想得到的结果如下:

id name

3 test3

1 test1

5 test5

请问在这样的SQL在Mysql中怎么写?

网上查到sqlserver中可以用order by charindex解决,但是没看到Mysql怎么解决??请高手帮忙,谢

谢!

select * from a order by substring_index('3,1,2',id,1)

使用mysql时,通常表中会有一个自增的id字段,但当我们想将表中的数据清空重新添加数据时,希望id重新从1开始计数,用以下两种方法均可:

方法一:

alter table tablename drop column id

alter table tablename add id mediumint(8) not null primary key auto_increment first

方法二:

alter table tablename auto_increment=0

方法二不会清空已有数据, *** 作比较灵活,不仅可以将自增值归零,也适用于删除大量连续行后,重新设置自增值并插入新的数据;或从新的值开始,当然不能和已有的冲突。 

$sql="delete from $table_vote" 

mysql_query($sql, $link) 

$sql="alter table $table_vote auto_increment=1" 

mysql_query($sql, $link)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8602421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-18
下一篇 2023-04-18

发表评论

登录后才能评论

评论列表(0条)

保存