Questions tagged [mysql-error-1242]

Error 1242: Subquery returns more than 1 row

When a subquery returns more than one row in a context where one row is expected, you get the following error:

Error 1242: Subquery returns more than 1 row

Consider for example the following query that returns the row in yourtable with the maximum associated id:

SELECT yourtable.*
FROM yourtable
WHERE id = (SELECT MAX(id) FROM yourtable)

This works fine because the subquery returns no more than one row.

You might then want to obtain all rows with the maximum id for every name, so you have to add a GROUP BY clause in the subquery, but you also have to modify the outer query this way:

SELECT yourtable.*
FROM yourtable
WHERE id IN (SELECT MAX(id)
             FROM yourtable
             GROUP BY name)

(here an IN clause is needed because the subquery could return more than one row).

Or you can always use a LIMIT 1, like in this example that returns the row with the maximum id for the greatest name:

SELECT *
FROM yourtable
WHERE id = (SELECT MAX(id)
            FROM yourtable
            GROUP BY name
            ORDER BY name DESC
            LIMIT 1)
77 questions
57
votes
5 answers

Using GROUP_CONCAT on subquery in MySQL

I have a MySQL query in which I want to include a list of ID's from another table. On the website, people are able to add certain items, and people can then add those items to their favourites. I basically want to get the list of ID's of people who…
Aistina
  • 12,435
  • 13
  • 69
  • 89
12
votes
3 answers

Multiple rows output into variables in MySQL

The following query seems to only work when there is one single entry with dateOfBirth set to 1975-02-28. It fails when there are multiple records matching this condition. Is it possible to delete all the addresses of users whose attribute…
Raju
  • 249
  • 1
  • 3
  • 13
11
votes
3 answers

MySQL Benchmark

I am trying to use MySQL benchmark to test some queries. But, I am running to an error. SELECT benchmark (10000, (select title from user)); and in return I get this error; ERROR 1242 (21000): Subquery returns more than 1 row Does anyone know how…
user239756
  • 113
  • 1
  • 1
  • 4
10
votes
4 answers

#1242 - Subquery returns more than 1 row - mysql

I am trying to make a select statement that selects the image names from a MySQL database. The table is called - pictures_archive. I am also trying to select these pictures depending on the category they have. The code is: SELECT…
Apostrofix
  • 2,140
  • 8
  • 44
  • 71
5
votes
5 answers

Can't get head round mysql subquery

Im having trouble getting my head round subqueries in Mysql. Fairly simple ones are ok, and most tutorials I find rarely go beyond the typical: SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2); What I am trying to pull out of my database…
prevailrob
  • 275
  • 3
  • 11
4
votes
3 answers

if "Subquery returns more than 1 row" consider it NULL

I'm trying to sync store ids on newtable with the ids from the maintable here: UPDATE newtable t SET t.store_id = (SELECT store_id FROM maintable s WHERE t.state = s.state AND s.city = t.city AND t.name = s.name) Whenever a subquery returns more…
Caio Iglesias
  • 594
  • 9
  • 24
4
votes
3 answers

How to overcome MySQL 'Subquery returns more than 1 row' error and select all the relevant records

Table creation CREATE TABLE `users` ( `id` INT UNSIGNED NOT NULL, `name` VARCHAR(100) NOT NULL, PRIMARY KEY(`id`) ); CREATE TABLE `email_address` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` INT UNSIGNED NOT NULL, …
Channa
  • 4,963
  • 14
  • 65
  • 97
3
votes
2 answers

MySQL update errors with "Subquery returns more than 1 row" despite lack of subquery

I have a query on mysql 5.1.56: select * from team_member_accounts where node = 33136 ...that returns exactly one row. However, the following query errors with Subquery returns more than 1 row: update team_member_accounts set fee_remaining = 0, …
Kev
  • 15,899
  • 15
  • 79
  • 112
3
votes
1 answer

Django subquery problem "Subquery returns more than 1 row"

I have three related models like class City(models.Model): name = models.CharField(max_length=200, blank=False) country = models.ForeignKey(Country,unique=False,null=False) def __unicode__(self): return self.name class…
brsbilgic
  • 11,613
  • 16
  • 64
  • 94
2
votes
4 answers

error : #1242 - Subquery returns more than 1 row

I got an error: #1242 - Subquery returns more than 1 row when i run this sql. CREATE VIEW test AS SELECT cc_name, COUNT() AS total, (SELECT COUNT(*) FROM bed WHERE respatient_id > 0 GROUP BY…
user705884
  • 21
  • 1
  • 1
  • 2
2
votes
6 answers

Selecting with subqueries in MySQL (Subqueries with ANY, and IN)

Thanks for the great answers! For More Information More info on MySQL IN comparison operator Joining Tables Subqueries with ANY, IN, and SOME This is hard to explain, so lets set the stage... userActions userGroupMap +------+--------+ …
Blaine
  • 1,107
  • 4
  • 12
  • 23
2
votes
2 answers

MySQL error : #1242 - Subquery returns more than 1 row

SELECT BookId, Duedate, (SELECT Title FROM Book_Information WHERE BookId = BookId) FROM Transaction_Information WHERE DueDate <= CURDATE() AND ReturnedDate IS NULL The book id is the foreign key in the transaction_information. I want to add the…
progammer101
  • 47
  • 1
  • 8
2
votes
1 answer

Insert into w/ multiple selects giving ERROR 1242: Subquery returns more than 1 row

I have table foo1 with columns UserID,TimeStamp; foo2 with columns userID,Level & table foo3 with columns userID,Timestamp. I want to INSERT into foo1 all rows from foo3 where the UserID exists in table foo2. I am getting ERROR 1242: Subquery…
Ram-man
  • 27
  • 5
1
vote
6 answers

SQL SELECT Question

I am trying to perform a SQL query that acts in two parts. First, I have a query that returns a list of 10 Ids. But then I want to have a SELECT statement which has a WHERE clause for each of these 10 ids. Is this possible? I tried: SELECT *…
Brett
  • 11,637
  • 34
  • 127
  • 213
1
vote
3 answers

Subquery returns more than 1 row in mysql

I am executing the following query and got the error "Subquery returns more than 1 row" My Query is SELECT pkTicketID, TicketReplyDateAdded, TIMESTAMPDIFF(DAY, TicketReplyDateAdded, now()) as NumberOfDays FROM tickets as T LEFT…
Umar Adil
  • 5,128
  • 6
  • 29
  • 47
1
2 3 4 5 6