我正在尝试使用MySQL将子查询结果存储在会话变量中,但问题是当我像这样执行此查询时(请参阅下面的查询),错误消息将显示如下(请参阅下面的错误消息)
我不知道如何解决这个问题。
(0 row(s) affected)
Execution Time : 00:00:00:343
Transfer Time : 00:00:00:000
Total Time : 00:00:00:343
Error Code : 1222
The used SELECT statements have a different number of columns
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
查询
SET @post := 0;
SELECT user_name,
(SELECT COUNT(*) INTO @post FROM post WHERE user_id = '23332'),
@post
FROM `user_fans` WHERE user_id = '23332';
答案 0 :(得分:1)
SET @post := 0;
SELECT user_name,
@post := (SELECT COUNT(*) FROM post WHERE user_id = '23332'),
@post
FROM `user_fans`
WHERE user_id = '23332';
答案 1 :(得分:0)
如果您只想进一步使用帖子计数,那就没关系了
如果您只想显示帖子数,请使用
SELECT user_name,
(SELECT COUNT(*) FROM post WHERE user_id = '23332') as Post,
FROM `user_fans`
WHERE user_id = '23332';