Solved

mysql, view, ERROR 1242 (21000): Subquery returns more than 1 row

Posted on 2012-04-08
2
885 Views
Last Modified: 2012-04-09
Hi can someone help me with this please,

i have a view where i am joining up several tables so is easier to query, i seem to be having this error stating subquery returns more than 1 row.

create view full_book_view as select b.book_id, b.book_title,b.isbn,b.retail_price, b.new_price, b.postage_price, b.description, b.quantity, b.book_condition
			    , (select group_concat(a.author) FROM book_authors as ba 
			    inner join author as a on ba.author_id = a.author_id
			    WHERE ba.book_id = b.book_id
			   GROUP BY ba.book_id) as Author,
			   (select group_concat(pub.publisher) from book_publishers as pb
			   inner join publisher as pub on pb.publisher_id = pub.publisher_id
			   where pb.book_id = b.book_id group by pb.book_id) as Publisher,
			   (select url from book_url as bu where bu.book_id = b.book_id)as url ,(select cust.username from customer as cust inner join users_per_book as upb on upb.customer_id = cust.customer_id where upb.book_id = b.book_id) as customer
			from books as b
			inner join book_categories as bc on bc.book_id = b.book_id
			inner join categories as cat on cat.cat_id = bc.category_id;

Open in new window


thanks
0
Comment
Question by:MK15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 37822322
Any one of the subqueries could result in returning more than value, but because you put them in the select clause, they need to always return only one value.  I guess you write your SQL this way because it was possible that a book's author, publisher, url, or customer is not defined?  Then you should use left outer joins.  Try:

create view full_book_view as 
select b.book_id, b.book_title,b.isbn,b.retail_price, b.new_price, b.postage_price, b.description, b.quantity, b.book_condition, 
    a.author, p.publisher, bu.url, c.username as customer
			from books as b
			inner join book_categories as bc on bc.book_id = b.book_id
			inner join categories as cat on cat.cat_id = bc.category_id
                        left outer join (select ba.book_id book_id, group_concat(a.author) author
                                         FROM book_authors as ba 
			                 inner join author as a on ba.author_id = a.author_id
			                 GROUP BY ba.book_id) a
                            on b.book_id = a.book_id
                        left outer join (select pb.book_id book_id, group_concat(pub.publisher) publisher 
                                        from book_publishers as pb
			                inner join publisher as pub on pb.publisher_id = pub.publisher_id
			                group by pb.book_id) p
                            on b.book_id = p.book_id
                        left outer join book_url bu on b.book_id = bu.book_id
                        left outer join (select upb.book_id book_id, cust.username 
                                         from customer as cust 
                                        inner join users_per_book as upb on upb.customer_id = cust.customer_id) c
                            on b.book_id = c.book_id;

Open in new window


NOTE: obviously untested, but should give you a good idea how you should proceed from here.
0
 

Author Comment

by:MK15
ID: 37824061
Hi,
thanks a lot mate go rid of the error
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
UPDATE query not working in mysqli php 8 102
Optimize the query 5 65
how to access a remote mysql database with xampp 3 73
MySQL 6 48
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question