MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 17th, 2002, 01:33 AM
peeyush peeyush is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: India
Posts: 17 peeyush User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to peeyush
Subselect queries in MySQL

Hi

I was trying subselect queries in MySQL and it is giving me error. I believe my syntax is right, just check it

Select * from ItemMaster where ItemId = (Select ItemId from IssueMaster where IssueId = 1)

Can you suggest something to make subqueries work.
I am using version 3.23.42-nt of MySQL

Reply With Quote
  #2  
Old December 17th, 2002, 11:48 AM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
If I'm not mistake, subqueries aren't support yet in MySQL... I have a link that I found when I was trying the same thing. We used subqueries in Oracle, so I wanted to see if they were supported in MySQL. Check out this link.
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old April 22nd, 2005, 04:43 AM
hutty hutty is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 1 hutty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 58 sec
Reputation Power: 0
Late as this response is I hope it might help those of us who are still using an older MySql backend. I had a similar question relating to subselects in MySql.

As an example, lets say I have two tables. The first (GALLERY) and the second (IMAGES). The relationship between the two being:

1 : M, i.e. (1 - Gallery) -> (M - Images).

I wanted to produce a count of images associated to a particular gallery. I wanted this so I could show the user how many other images were associated with the one they were looking at as well as show 'Next' and 'Previous' links along with the count. Lets assume that all I know is the images 'ID', i.e. (Images.ImageID). This is being passed to the page that shows the enlarged image from a thumbnail via a query string parameter.

So, you'd expect the SQL code to look something like this:

Code:
SELECT count(*) as all_associated_images
  FROM images
  WHERE images.gal_id in (
   SELECT gallery.gal_id
   FROM gallery g, images i
   WHERE g.gal_id = i.gal_id
   AND i.imageID = 988);


Note* ImageID 988 is just an arbitrary value.

This will, of course, not work.

However, the following code does work:

Code:
SELECT count(*) as all_associated_images
  FROM	images i INNER JOIN  gallery g  ON i.gal_id=g.gal_id
  AND i.imageID = 988
  INNER JOIN images i2 on i2.gal_id = i.gal_id


I know this comes a little late for the person who originally posted this question but hopefully it might help someone else.

Cheers,

Hutty

Reply With Quote
  #4  
Old April 22nd, 2005, 08:57 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 997 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2Folding Points: 906075 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 14 h 26 m 27 sec
Reputation Power: 5
Send a message via ICQ to Itsacon
Or you could upgrade to MySQL version 4.1.*, which DOES support subqueries.
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Last edited by Itsacon : October 6th, 2005 at 12:17 PM. Reason: typo...

Reply With Quote
  #5  
Old May 14th, 2005, 09:56 AM
gertcuppens's Avatar
gertcuppens gertcuppens is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 118 gertcuppens User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 28 m
Reputation Power: 5
Itsacon is right : subqueries are supported in MySQL 4.1
but not in MySQL 4.0. Which gave me a problem, since
my local database is a 4.1 and the database on the server is
a 4.0.
So, I had to rewrite my subqueries.

Reply With Quote
  #6  
Old October 6th, 2005, 12:03 PM
macroguru macroguru is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 1 macroguru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 41 sec
Reputation Power: 0
So I am new to MySQL and I wrote my query using a subselect....well, I found out today, the company I am at is using an older version of MySQL...

So I am trying to figure out how to take my query and write it without using the subselect

Code:
SELECT DISTINCT ci.cust_id, ci.first_name, ci.last_name, ci.home_phone, ci.work_phone, ci.mobile,ci.email, ls.lead_source, pi.product_name, oi.order_date, oi.order_id, opd.prod_price
FROM cust_info ci LEFT JOIN order_info oi 
 ON ci.cust_id = oi.cust_id
 LEFT JOIN order_product_details opd
 ON oi.order_id = opd.order_id
 LEFT JOIN lead_source ls
 ON ci.lead_source_id = ls.lead_source_id
 LEFT JOIN product_info pi
 ON opd.product_id = pi.product_id
WHERE oi.order_status_id IN (1,5) 
AND
 oi.order_date >= CURRENT_DATE() -60
AND ci.custi_d not in (select ci2.cust_id from cust_info ci2 LEFT JOIN order_info oi2
                                ON ci.cust_id = oi.cust_id
                                LEFT JOIN order_product_details opd2
                                ON oi2.order_id = opd2.order_id
                                WHERE opd2.product_id IN (6,16,26)) 
Group By ci.cust_id 


Any help is appreciated....

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Subselect queries in MySQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT