If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help with subselect

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-07-07, 07:57
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Help with subselect

Hello everybody,

I'm trying to get the sql below to work but apparently the syntax isn't quite right:

Code:
SELECT model, (SELECT MAX(model_id) FROM table_a) AS max_id
FROM table_b
WHERE table_b.model_id=max_id
The error message I got is: Unknown column max_id in WHERE clause.

Could some point me in the right direction?

Thanks in anticipation

Last edited by pearl2; 12-07-07 at 08:08.
Reply With Quote
  #2 (permalink)  
Old 12-07-07, 14:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
could you please do SHOW CREATE TABLE for both your tables

couple rows of sample data wouldn't hurt either
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-07-07, 19:48
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Hi r937,

Thanks, here is the create table code:

Code:
CREATE TABLE table_a (
  `model_id` smallint(5) unsigned PRIMARY KEY NOT NULL auto_increment,
  `description` tinytext NOT NULL default '',
  `created` date NOT NULL default '0000-00-00',
);

CREATE TABLE table_b (
  `user_id` smallint(5) unsigned PRIMARY KEY NOT NULL auto_increment,
  `model` varchar(50) NOT NULL default '',
  `description` text NOT NULL default '',
  `model_id` smallint(5) unsigned NOT NULL,
  `registered` date NOT NULL default '0000-00-00',
);
I haven't got any data as I'm still experimenting with the table setup.

Last edited by pearl2; 12-07-07 at 20:36.
Reply With Quote
  #4 (permalink)  
Old 12-07-07, 20:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
okay, that's great, thanks

now, could you please explain what you wanted the query to do?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 12-07-07, 20:39
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Quote:
Originally Posted by r937
okay, that's great, thanks

now, could you please explain what you wanted the query to do?
I wanted to select data from table_b based on the maximum model_id from table_a.

This works:

Code:
SELECT model FROM table_b
WHERE table_b.model_id=(SELECT MAX(model_id) FROM table_a)
But the max_id isn't selected...
Reply With Quote
  #6 (permalink)  
Old 12-07-07, 20:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select user_id
     , model
     , description
     , model_id
     , registered
  from table_b 
 where model_id =
       ( select max(model_id)
           from table_a )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 12-07-07, 21:03
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Quote:
Originally Posted by r937
Code:
select user_id
     , model
     , description
     , model_id
     , registered
  from table_b 
 where model_id =
       ( select max(model_id)
           from table_a )
Thanks, r937!



Added:

But is it possible to write the sql so that even if no row exist in table b meets the condition, the max_id from table_a is still selected. Is that doable?

Last edited by pearl2; 12-07-07 at 21:12.
Reply With Quote
  #8 (permalink)  
Old 12-07-07, 22:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
use two queries
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 12-07-07, 22:55
pearl2 pearl2 is offline
Registered User
 
Join Date: Nov 2003
Location: Sinapore
Posts: 187
Quote:
Originally Posted by r937
use two queries
Ok thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On