gpt4 book ai didi

mysql - 如何处理这个错误 1242 (21000) : Subquery returns more than 1 row

转载 作者:行者123 更新时间:2023-11-29 01:29:29 25 4
gpt4 key购买 nike

大家好,我在使用 SQL 查询时遇到了问题,用谷歌搜索了很多但找不到任何解决方案。这是我的代码:

 SELECT (SELECT ltp 
FROM eod_stock
WHERE company_id IN (1,2)
AND entry_date = '2013-09-24') AS b,
(SELECT ltp
FROM eod_stock
WHERE company_id IN (1,2)
AND entry_date = '2013-09-25') AS e,
CAST(((SELECT ltp
FROM eod_stock
WHERE company_id IN(1,2)
AND entry_date = '2013-09-25') -
(SELECT ltp
FROM eod_stock
WHERE company_id IN (1,2)
AND entry_date = '2013-09-24')) /
(SELECT ltp
FROM eod_stock
WHERE company_id IN (1,2)
AND entry_date = '2013-09-25')*100
AS DECIMAL(10,2)) AS c;

它给我错误

ERROR 1242 (21000): Subquery returns more than 1 row

我理解错误导致我的查询应该返回超过 1 行,因为我正在传递 2 个公司,但我需要所有公司 ltp,我在查询中提到。结果是需要像这样:

+--------+--------+--------+
| b | e | c |
+--------+--------+--------+
| 460.00 | 338.00 | -36.09 |
+--------+--------+--------+
| 320.00 | 488.00 | 34.42 |
+--------+--------+--------+

谁能建议我如何从我的查询中获得这样的多个结果?因为我必须在查询中传递超过 1 个公司 ID。

表结构:

+-----------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| company_id | varchar(30) | NO | PRI | NULL | |
| entry_date | date | NO | PRI | NULL | |
| entry_timestamp | int(10) unsigned | NO | | NULL | |
| open | decimal(16,2) | NO | | NULL | |
| high | decimal(16,2) | NO | | NULL | |
| low | decimal(16,2) | NO | | NULL | |
| ltp | decimal(16,2) | NO | | NULL | |
| ycp | decimal(16,2) | NO | | NULL | |
| cse_price | decimal(9,2) | NO | | NULL | |
| cse_volume | decimal(18,2) | NO | | NULL | |
| total_trade | int(30) | NO | | NULL | |
| total_volume | int(30) | NO | | NULL | |
| total_value | decimal(18,4) | NO | | NULL | |
+-----------------+------------------+------+-----+---------+-------+

最佳答案

当您从子查询中选择时,您只能得到一行。

您可以加入表格本身并选择包含您感兴趣的两个日期的行。这样的事情应该有效。

SELECT a.ltp, b.ltp, CAST(((b.ltp - a.ltp) / b.ltp * 100) AS DECIMAL(10, 2))
FROM eod_stock a
JOIN eod_stock b ON a.company_id = b.company_id
WHERE a.entry_date = "2013-09-24"
AND b.entry_date = "2013-09-25"
AND a.company_id IN (1, 2)

关于mysql - 如何处理这个错误 1242 (21000) : Subquery returns more than 1 row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21141776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com