MySQL:错误代码:1242 子查询 returns 多于 1 行
MySQL: Error Code: 1242 Subquery returns more than 1 row
SELECT daire.code, daire.durumu, daire.blblkodu, CONCAT(cari.adi , cari.soyadi) AS sahibi,
CASE daire.blkrcrkodu WHEN '0'
THEN CONCAT(cari.adi,cari.soyadi)
ELSE (SELECT CONCAT(adi,soyadi)
FROM cari
WHERE blkodu = daire.blkrcrkodu
AND firma_code = 40
AND site_code = 84) END AS oturan,
daire.kat, daire.kapi_no, daire.giris_no, daire.alan_m2, daire.__formatted_date
FROM daire
INNER JOIN cari ON
(daire.blshcrkodu = cari.blkodu AND cari.site_code = 84 AND daire.site_code = 84)
WHERE daire.site_code = 84
AND daire.firma_code = 40
ORDER BY daire.blkodu DESC
我有语法错误吗?我不明白是什么问题,你能帮我吗?
如果 subselect return 超过一行但是你需要例如第一个你可以使用 limit 1
SELECT daire.code, daire.durumu, daire.blblkodu, CONCAT(cari.adi , cari.soyadi) AS sahibi,
CASE daire.blkrcrkodu WHEN '0'
THEN CONCAT(cari.adi,cari.soyadi)
ELSE (SELECT CONCAT(adi,soyadi)
FROM cari
WHERE blkodu = daire.blkrcrkodu
AND firma_code = 40
AND site_code = 84
LIMIT 1) END AS oturan,
daire.kat, daire.kapi_no, daire.giris_no, daire.alan_m2, daire.__formatted_date
FROM daire
INNER JOIN cari ON
(daire.blshcrkodu = cari.blkodu AND cari.site_code = 84 AND daire.site_code = 84)
WHERE daire.site_code = 84
AND daire.firma_code = 40
ORDER BY daire.blkodu DESC
您在主查询的 select 列表中有一个子查询:
...
(SELECT CONCAT(adi,soyadi)
FROM cari
WHERE blkodu = daire.blkrcrkodu
AND firma_code = 40
AND site_code = 84) END AS outran
...
此类子查询必须 return 1 行 1 列(单个值)。您的子查询 return 有 1 列,但 return 超过 1 行,因此出现错误。
关于如何修改查询以满足您的需要,我无法为您提供确切的解决方案,因为您没有分享任何关于查询本身的信息,而且您只是询问了错误是什么。一般可以申请
limit 1
子句将行数限制为 1
- 使用任何聚合函数,例如
min()
将结果集折叠成一行
- 将您的 where 条件修改为 return 仅一条记录。
SELECT daire.code, daire.durumu, daire.blblkodu, CONCAT(cari.adi , cari.soyadi) AS sahibi,
CASE daire.blkrcrkodu WHEN '0'
THEN CONCAT(cari.adi,cari.soyadi)
ELSE (SELECT CONCAT(adi,soyadi)
FROM cari
WHERE blkodu = daire.blkrcrkodu
AND firma_code = 40
AND site_code = 84) END AS oturan,
daire.kat, daire.kapi_no, daire.giris_no, daire.alan_m2, daire.__formatted_date
FROM daire
INNER JOIN cari ON
(daire.blshcrkodu = cari.blkodu AND cari.site_code = 84 AND daire.site_code = 84)
WHERE daire.site_code = 84
AND daire.firma_code = 40
ORDER BY daire.blkodu DESC
我有语法错误吗?我不明白是什么问题,你能帮我吗?
如果 subselect return 超过一行但是你需要例如第一个你可以使用 limit 1
SELECT daire.code, daire.durumu, daire.blblkodu, CONCAT(cari.adi , cari.soyadi) AS sahibi,
CASE daire.blkrcrkodu WHEN '0'
THEN CONCAT(cari.adi,cari.soyadi)
ELSE (SELECT CONCAT(adi,soyadi)
FROM cari
WHERE blkodu = daire.blkrcrkodu
AND firma_code = 40
AND site_code = 84
LIMIT 1) END AS oturan,
daire.kat, daire.kapi_no, daire.giris_no, daire.alan_m2, daire.__formatted_date
FROM daire
INNER JOIN cari ON
(daire.blshcrkodu = cari.blkodu AND cari.site_code = 84 AND daire.site_code = 84)
WHERE daire.site_code = 84
AND daire.firma_code = 40
ORDER BY daire.blkodu DESC
您在主查询的 select 列表中有一个子查询:
...
(SELECT CONCAT(adi,soyadi)
FROM cari
WHERE blkodu = daire.blkrcrkodu
AND firma_code = 40
AND site_code = 84) END AS outran
...
此类子查询必须 return 1 行 1 列(单个值)。您的子查询 return 有 1 列,但 return 超过 1 行,因此出现错误。
关于如何修改查询以满足您的需要,我无法为您提供确切的解决方案,因为您没有分享任何关于查询本身的信息,而且您只是询问了错误是什么。一般可以申请
limit 1
子句将行数限制为 1- 使用任何聚合函数,例如
min()
将结果集折叠成一行 - 将您的 where 条件修改为 return 仅一条记录。