• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

How to create a proper Nested select statement in mysql

I am getting the error message "subquery returns more than one row" with the code below? I am trying to select only those records from the table shosondemand who's id is in the tmp_shoe_id table.   Whats wrong with the code?
select distinct * from shosondemand where id = (select *  from tmp_shoe_id);

Open in new window

3 Solutions
Kent OlsenData Warehouse Architect / DBACommented:
Hi Dan,

There are several ways to do this.  Most common is to use IN.

select distinct * from shosondemand where id IN (select id  from tmp_shoe_id);

Good Luck,
Sudaraka WijesingheWeb Application ProgrammerCommented:
If you need to use the = (equal sign) you need to make sure the second query only returns a single value.
I like this, but admit it's equivalent to Dan's query.  The advantage is that it allows you to select fields from the tmp_shoe table if you like:

Select s.* from shosondemand s inner join tmp_shoe_id t on t.id = s.id
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now