mysql中的子查询错误

时间:2014-03-26 18:06:54

标签: mysql join

SELECT
  c.cy_id AS cy_id,
  (SELECT 
     c.to_id
   FROM c
   WHERE (c.to_id = se.ar))AS tsn,
  (SELECT
     se.x
   FROM se
   WHERE (c.to_id = se.ar)) AS X,
  (SELECT
     se.y
   FROM se
   WHERE (c.to_id = se.ar)) AS y
FROM (c
   JOIN se)
WHERE (c.to_id = se.ar)

当我在mysql中执行时,上面的查询给出了错误子查询返回多行。 但是,当我执行以下查询时,它会返回结果。

SELECT
  c.cy_id AS cy_id,
  (SELECT DISTINCT
     c.to_id
   FROM c
   WHERE (c.to_id = se.ar))AS tsn,
  (SELECT
     se.x
   FROM se
   WHERE (c.to_id = se.ar)) AS X,
  (SELECT
     se.y
   FROM se
   WHERE (c.to_id = se.ar)) AS y
FROM (c
   JOIN se)
WHERE (c.to_id = se.ar).

我需要一个理由。

1 个答案:

答案 0 :(得分:0)

您的c表似乎有多行,但它们都具有相同的to_id值。如果子查询返回多个记录,则会出现错误。

此外,您的查询似乎不需要任何子查询。试试这个,看看它能不能得到你想要的结果:

SELECT
  c.cy_id,
  c.to_id AS tsn,
  se.x AS X,
  se.y AS y
FROM c
   JOIN se
      ON c.to_id = se.ar