|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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... |
|
#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. |
|
#6
|
|||
|
|||
|
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.... |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > Subselect queries in MySQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|