如何将select结果赋值到一个变量里?

shisoftgenius 2010-03-23 01:57:09
比如 select "name" from serviceclientsinfo.conactnametable where owner='b'
我希望出来的结果的第一行赋值给变量id?
...全文
1255 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuoxingyu 2010-07-20
  • 打赏
  • 举报
回复

mysql> select name into @A from area limit 1;
Query OK, 1 row affected (0.00 sec)

mysql> select @A;
+------+
| @A |
+------+
| 北京 |
+------+
1 row in set (0.00 sec)

mysql>


不过这样的@a是session级别的,只能在当前连接下访问。
luketty 2010-07-20
  • 打赏
  • 举报
回复
2楼 正解
urakvv7 2010-07-19
  • 打赏
  • 举报
回复
SELECT @varname=columnname FROM tablename

上述语句在SQL SERVER 2008里可执行通过。
liqiang8 2010-03-30
  • 打赏
  • 举报
回复
试试select @name:=name from tablename where id=1
shisoftgenius 2010-03-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acmain_chm 的回复:]

select colName into @var from table1 where id=10
[/Quote]
insert into serviceclientsinfo.conactnametable ("name","owner") values ('a','b');
select "name" into @var from serviceclientsinfo.conactnametable where owner='b';
select @var;

我用的是postgresql,使用上面语句报错ERROR: syntax error at or near "@"
xxy_aldrich 2010-03-23
  • 打赏
  • 举报
回复
学习了。。。。
ACMAIN_CHM 2010-03-23
  • 打赏
  • 举报
回复
MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html
[Quote]mysql> delimiter //

mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;

mysql> CALL simpleproc(@a);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @a;
+------+
| @a |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
[/Quote]
ACMAIN_CHM 2010-03-23
  • 打赏
  • 举报
回复
select colName into @var from table1 where id=10

56,678

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧