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, 12: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, 10: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: 9
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, 03: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, 07: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: 1,030 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: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4Folding Points: 1978056 Folding Title: Super Ultimate Folder - Level 4
Time spent in forums: 1 Week 9 h 13 m 59 sec
Reputation Power: 7
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: 564

Last edited by Itsacon : October 6th, 2005 at 11:17 AM. Reason: typo...

Reply With Quote
  #5  
Old May 14th, 2005, 08: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: 6
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, 11:03 AM
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
  #7  
Old January 19th, 2010, 05:31 AM
ComCic ComCic is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 1 ComCic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 7 sec
Reputation Power: 0
Hi! I have a problem with this code...

I'm using Mysql 4.0.3 and i can't update to 4.1

mdTemp.CommandText = " select oc.*, r.codfig, r.cognom, prj.codprj from OreConsuntive oc, Risorse r, PrjProgetti prj, PrjTask prjt where oc.dt_cns between '"& DataInizioDB &"' and '"& DataFineDB &"' "_
& " and oc.codtsk in (SELECT pt.codtsk from PrjTask pt, PrjProgetti prj where prj.cod_budget='"& codicebudget &"' and prj.codprj=pt.codprj) "_
& " and oc.fl_rec<>'I' and oc.codcop='F00' and oc.codtsk=prjt.codtsk and prjt.codprj=prj.codprj" _
& " and r.tipoat='20' and prj.fl_prj<>'F'and oc.iduser=r.iduser "


The problem is in "IN" state ( i think Mysql 4.0.3 doesn't support it. The error message is

"Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[MySQL][ODBC 3.51 Driver][mysqld-4.0.30-standard-log]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT pt.codtsk from PrjTask pt, PrjProgetti prj where prj.cod
"

how can i solve it ?? i have to use Inner Join ?? If yes..how can i "translate"...

Thank you very much for any kind of help

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




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

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




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek