MySQL subquery returns more than one row

前端 未结 4 2095
粉色の甜心
粉色の甜心 2020-12-01 16:22

I am executing this query:

SELECT
    voterfile_county.Name,
    voterfile_precienct.PREC_ID,
    voterfile_precienct.Name,
    COUNT((SELECT voterfile_vote         


        
相关标签:
4条回答
  • 2020-12-01 16:51

    You can try it without the subquery, with a simple group by:

    SELECT voterfile_county.Name, 
      voterfile_precienct.PREC_ID, 
      voterfile_precienct.Name, 
      count(voterfile_voter.ID)
    FROM voterfile_county
    JOIN voterfile_precienct 
      ON voterfile_precienct.County_ID = voterfile_County.ID
    JOIN voterfile_household 
      ON voterfile_household.Precnum = voterfile_precienct.PREC_ID
    JOIN voterfile_voter 
      ON voterfile_voter.House_ID = voterfile_household.ID 
    GROUP BY voterfile_county.Name, 
      voterfile_precienct.PREC_ID, 
      voterfile_precienct.Name
    

    When you use GROUP BY, any column that you are not grouping on must have an aggregate clause (f.e. SUM or COUNT.) So in this case you have to group on county name, precienct.id and precient.name.

    0 讨论(0)
  • 2020-12-01 17:01

    See the below example and modify your query accordingly.

    select COUNT(ResultTPLAlias.id) from 
    (select id from Table_name where .... ) ResultTPLAlias;
    
    0 讨论(0)
  • 2020-12-01 17:04

    Try this

    SELECT
    voterfile_county.Name, voterfile_precienct.PREC_ID, 
    voterfile_precienct.Name,
        (SELECT COUNT(voterfile_voter.ID) 
        FROM voterfile_voter JOIN voterfile_household
        WHERE voterfile_voter.House_ID = voterfile_household.ID
          AND voterfile_household.Precnum = voterfile_precienct.PREC_ID) as Voters
    FROM voterfile_precienct JOIN voterfile_county 
    ON voterfile_precienct.County_ID = voterfile_County.ID
    
    0 讨论(0)
  • 2020-12-01 17:08

    If you get error:error no 1242 Subquery returns more than one row, try to put ANY before your subquery. Eg:

    This query return error:

    SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
    

    This is good query:

    SELECT * FROM t1 WHERE column1 = ANY (SELECT column1 FROM t2);
    
    0 讨论(0)
提交回复
热议问题