Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 14th, 2010, 10:16 AM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy SELECT subqueries

SELECT
(SELECT a.value FROM custom_table a WHERE a.bug_id = i.bug_id AND a.field_id = 4),
(SELECT b.value FROM custom_table b WHERE b.bug_id = i.bug_id AND b.field_id = 5),
(SELECT c.value FROM custom_table c WHERE c.bug_id = i.bug_id AND c.field_id = 6)
FROM custom_table i
WHERE i.bug_id = 27982


I got error, with code above....helppp...can anybody correct it.
 
Old September 14th, 2010, 06:18 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Again, what version of MySQL are you using.

And, again, why are you using a subquery in the first place???

Why not keep it simple?
Code:
SELECT value 
FROM custom_table
WHERE bug_id = 27982
AND field_id IN (4,5,6)
Hmmm...if you truly need all 3 values in a single record, then I guess you could do it as
Code:
SELECT a.value, b.value, c.value
FROM custom_table AS a, custom_table AS b, custom_table AS c
WHERE a.bug_id = 27982
AND b.bug_id = a.bug_id
AND c.bug_id = a.bug_id
AND a.field_id = 4
AND b.field_id = 5
AND c.field_id = 6
But see, still no subquery needed.
 
Old September 15th, 2010, 01:56 AM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the advice, now I already got all 3 values in a single record, and all this 3 fields should related with MAX(bugnote_text_id) value.

Let say :-

a b c 1
d e f 2
g h i 3


It should come out :-

g h i 3
 
Old September 15th, 2010, 06:37 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Fuhh ! i still cannot get fields to follow with MAX(bugnote_text_id), any advice on this.
 
Old September 15th, 2010, 07:51 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, the best advice I can give is to move to MySQL Version 5.1.

4.0 is really very very old, and it's way past time to move on.
 
Old September 15th, 2010, 09:27 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Old Pedant.





Similar Threads
Thread Thread Starter Forum Replies Last Post
SubQueries debbiecoates SQL Server 2000 5 October 22nd, 2007 02:04 PM
SubQueries in a select statement in dataset madhusrp Reporting Services 1 February 22nd, 2007 03:03 AM
Multiple subqueries? thf1977 MySQL 1 October 24th, 2006 05:30 PM
Trouble with Subqueries, am I going mad. kim3er SQL Server 2000 3 April 10th, 2005 02:13 PM
subqueries mgdts SQL Server DTS 0 July 28th, 2003 01:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.