mysql|变量
我觉得很悲哀,为什么看的人多,说的人少呢。
是觉得太简单而不屑一顾呢?还是想留一小手呢?
问题的提出 id=2131117
有一个表,内容如下
id name
1 aaa
4 bbb
6 ccc
14 a1
15 a2
26 b1
怎样做才能得到如下结果
id name p
1 aaa 1
4 bbb 2
6 ccc 3
14 a1 4
15 a2 5
26 b1 6
又怎样做,才能这样?
name p
aaa aaa
bbb aaa bbb
ccc aaa bbb ccc
a1 aaa bbb ccc a1
a2 aaa bbb ccc a1 a2
b1 aaa bbb ccc a1 a2 b1
MySQL支持线程特定的变量,用@variablename句法。一个变量名可以由当前字符集的数字字母字符和“_”、“$”和“.”组成。缺省字符集是ISO-8859-1 Latin1;这可以通过重新编译MySQL改变。
变量不必被初始化。缺省地,他们包含NULL并能存储整数、实数或一个字符串值。当线程退出时,对于一个线程的所有变量自动地被释放。
你可以用SET句法设置一个变量:
SET @variable= { integer expression | real expression | string expression }
[,@variable= ...].
你也可以用@variable:=expr句法在一个表达式中设置一个变量:
select @t1:=(@t2:=1)+@t3:=4,@t1,@t2,@t3;
+----------------------+------+------+------+
| @t1:=(@t2:=1)+@t3:=4 | @t1 | @t2 | @t3 |
+----------------------+------+------+------+
| 5 | 5 | 1 | 4 |
+----------------------+------+------+------+
问题的解决:
1、
$rs = mysql_query("set @t=0");
$rs = mysql_query("SELECT id,name,@t:=@t+1 as p FROM tbl_name");
mysql_result_all($rs);
2、
$rs = mysql_query("set @t=''");
$rs = mysql_query("SELECT name,@t:=concat(@t,' ',name) as p FROM tbl_name");
mysql_result_all($rs);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
