我试图一次运行两个sql select语句,一个用于计算行数,另一个用于返回" search_names"中的值。我收到错误"子查询返回多行"因为rowcount只返回一个值,而searchresults返回许多值。有没有办法一起运行这些声明,还是我需要拆分它们?
SELECT(
SELECT COUNT(*)
FROM user_searches
WHERE `user_id` = 15
AND `read` = 0
)
AS rowcount,
(
SELECT `search_name`
FROM user_searches
WHERE `user_id` = 15
)
AS searchresults
答案 0 :(得分:1)
怎么样:
SELECT
(SELECT COUNT(*) FROM user_searches WHERE `user_id` = 15 AND `read` = 0)
AS rowcount,
GROUP_CONCAT(`search_name`) AS searchresults
FROM user_searches WHERE `user_id` = 15
问题是你不能连续返回整个数据集。但是您可以返回所有搜索名称的逗号分隔列表。然后,从您的程序中,您可以轻松地解析此列表。