MySQL: Error Code: 1242 Subquery returns more than 1 row

柔情痞子 提交于 2019-12-01 13:26:08

问题


error image here

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

Do I have a syntax error? I do not understand what the problem is, can you help me?


回答1:


If the subselect return more then a row but you need eg the first you can use 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



回答2:


You have a subquery in the select list of the main query:

...
(SELECT CONCAT(adi,soyadi)
 FROM cari
 WHERE blkodu = daire.blkrcrkodu
 AND firma_code = 40
 AND site_code = 84) END AS outran

...

Such subqueries must return 1 row and 1 column (single value). Your subquery returns 1 column, but returns more than 1 row, hence the error.

I cannot provide you an exact solution as to how to modify the query to suit your needs because you have not shared anything about the query itself and you only asked for what the error was anyway. Generally, you can apply

  • limit 1 clause to limit the number of rows to 1
  • use any aggregate function such as min() to collapse the resultset into a single row
  • modify your where criteria to return a single record only.


来源:https://stackoverflow.com/questions/39920179/mysql-error-code-1242-subquery-returns-more-than-1-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!