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:42 AM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default SELECT subqueries

SELECT id, bug_id, reporter_id, bugnote_text_id
FROM bugnote_table P1
WHERE bugnote_text_id = (SELECT MAX(bugnote_text_id) FROM bugnote_table P2 WHERE P2.id = P1.id)


I got error #1064 - 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 MAX(bugnote_text_id) FROM bugnote_table P2 WHERE #1064 - 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 MAX(bugnote_text_id) FROM mantis_bugnote_table P2 WHERE




Somebody correct it....help
 
Old September 14th, 2010, 05:55 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

What version of MySQL are you using?? You need at least version 5 to use subqueries.

But you know, there is NO reason to use a subquery for this simple question.

Code:
SELECT id, bug_id, reporter_id, bugnote_text_id 
FROM bugnote_table
ORDER BY bugnote_text_id DESC
LIMIT 1
 
Old September 15th, 2010, 01:27 AM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much for your kindly adviced and guide Old Pedant,

I am using My SQL 4.0.26, for your known I am using multiple tables and try to capture all fields need related with MAX(bugnote_text_id) field.

When I used code below, GROUP BY i.id, it come out with first record bugnote_text_id but I only want the MAX record of bugnote_text_id to come out.


SELECT i.id AS 'id', iii.note AS 'Solution', iv.value AS 'State'
FROM bug_table i, bugnote_table ii, bugnote_text_table iii, custom_field_string_table iv
WHERE i.id = ii.bug_id
AND ii.bugnote_text_id = iii.id
AND i.id = iv.bug_id
AND iv.field_id = 5
AND iv.value = 'KL'
AND i.date_submitted > '2010-08-01'AND i.date_submitted < '2010-09-01'
GROUP BY i.id
ORDER BY i.id ASC


Any advice with it.
Regards.
 
Old September 15th, 2010, 07:54 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, this *MIGHT* work:

Code:
SELECT i.id AS 'id', iii.note AS 'Solution', iv.value AS 'State'
FROM bug_table i, bugnote_table ii, bugnote_text_table iii, custom_field_string_table iv
WHERE i.id = ii.bug_id
AND ii.bugnote_text_id = iii.id
AND i.id = iv.bug_id
AND iv.field_id = 5
AND iv.value = 'KL'
AND i.date_submitted > '2010-08-01'AND i.date_submitted < '2010-09-01'
ORDER BY ii.bugnote_text_id DESC
LIMIT 1
But, really, my advice is still to move to MySQL 5.1.
 
Old September 15th, 2010, 09:39 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I will try yours, since now i couldnt get through the set will server...I hope it will produce every rows with MAX(ii.bugnote_text) and follow with other fields related.

Even I am using My SQL version 4.0, hope I can get ideas and solution how to workaround with it.


Thanks Old Pedant for your kindly help.
 
Old September 17th, 2010, 08:01 AM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

After tried code given, output only came out as 1 row because of using LIMIT 1.

The problem is 1 id got 100 bugnote_text_id together with fields related and i have 8000 id in table looking for it MAXIMUM bugnote_text_id each together with fields related.. (8000 rows).

But how to workaround with My SQL version 4.0, because I dont have any authority to upgrade 5.1.

Do anybody have ideas hellppppppppppppppppppp....
 
Old September 17th, 2010, 04:23 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

The best I can see to do is to use a SERIES of queries.

Code:
DROP TEMPORARY TABLE IF EXISTS top_bugnotes;

CREATE TEMPORARY TABLE top_bugnotes (
         bug_id INT,
    max_text_id INT );

INSERT INTO top_bugnotes( bug_id, max_text_id )
    SELECT bug_id, MAX(bugnote_text_id ) FROM bugnote_table
    GROUP BY bug_id;

SELECT B.id, BNT.note AS 'Solution', CFS.value AS 'State' 
FROM bug_table AS B, 
        bugnote_table AS BN, 
        top_bugnotes AS TB,
        bugnote_text_table AS BNT,
        custom_field_string_table AS CFS
WHERE BN.bug_id = B.id 
AND ( TB.bug_id = BN.bug_id AND TB.max_text_id = BN.bugnote_text_id)
AND BNT.id = BN.bugnote_text_id 
AND ( CFS.bug_id = B.id AND CFS.field_id = 5 AND CFS.value = 'KL' )
AND ( B.date_submitted > '2010-08-01' AND B.date_submitted < '2010-09-01' )
But just a guess.
 
Old September 20th, 2010, 11:32 PM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, thanks Old Pedant for your great ideas, I already got output that I need, using Temporary Table created as yours.But now, when I try to download to MS EXCEL, OUTPUT generated, errors occured said that temporary table used was not exitst.I warned by management not to create permanent table in database, just use it :(Any ideas to download it to MS EXCEL ? Happy to get the output but now I couldnt download it....arrgghhhh....what a world :)
 
Old September 21st, 2010, 02:33 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

A temporary table only exists as long as the connection exists. If you close the connection and open another one, then the temp table is gone.

I would *assume* that whatever code you are doing to create the EXCEL file is using a different connection. So... If at all possible, just move all the above code onto the SAME CONNECTION that you use to create the EXCEL data.
 
Old September 22nd, 2010, 09:19 AM
Authorized User
 
Join Date: Mar 2006
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok Okd Pedant, I code it using INTO OUTFILE 'f:/result.xls' in your example, file couldnt be create.... anything wrong here...

DROP TEMPORARY TABLE IF EXISTS top_bugnotes;

CREATE TEMPORARY TABLE top_bugnotes (
bug_id INT,
max_text_id INT );

INSERT INTO top_bugnotes( bug_id, max_text_id )
SELECT bug_id, MAX(bugnote_text_id ) FROM bugnote_table
GROUP BY bug_id;

SELECT B.id, BNT.note AS 'Solution', CFS.value AS 'State'

INTO OUTFILE 'f:/result.xls'

FROM bug_table AS B,
bugnote_table AS BN,
top_bugnotes AS TB,
bugnote_text_table AS BNT,
custom_field_string_table AS CFS
WHERE BN.bug_id = B.id
AND ( TB.bug_id = BN.bug_id AND TB.max_text_id = BN.bugnote_text_id)
AND BNT.id = BN.bugnote_text_id
AND ( CFS.bug_id = B.id AND CFS.field_id = 5 AND CFS.value = 'KL' )
AND ( B.date_submitted > '2010-08-01' AND B.date_submitted < '2010-09-01' )DROP TEMPORARY TABLE IF EXISTS top_bugnotes;

CREATE TEMPORARY TABLE top_bugnotes (
bug_id INT,
max_text_id INT );

INSERT INTO top_bugnotes( bug_id, max_text_id )
SELECT bug_id, MAX(bugnote_text_id ) FROM bugnote_table
GROUP BY bug_id;

SELECT B.id, BNT.note AS 'Solution', CFS.value AS 'State'
FROM bug_table AS B,
bugnote_table AS BN,
top_bugnotes AS TB,
bugnote_text_table AS BNT,
custom_field_string_table AS CFS
WHERE BN.bug_id = B.id
AND ( TB.bug_id = BN.bug_id AND TB.max_text_id = BN.bugnote_text_id)
AND BNT.id = BN.bugnote_text_id
AND ( CFS.bug_id = B.id AND CFS.field_id = 5 AND CFS.value = 'KL' )
AND ( B.date_submitted > '2010-08-01' AND B.date_submitted < '2010-09-01' )





Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT subqueries Wenggo MySQL 5 September 15th, 2010 09:27 PM
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
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.