| |
|
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.
|
 |

12-07-07, 07:57
|
|
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.
|

12-07-07, 14:00
|
|
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 
|
|

12-07-07, 19:48
|
|
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.
|

12-07-07, 20:21
|
|
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?
|
|

12-07-07, 20:39
|
|
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...
|
|

12-07-07, 20:50
|
|
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 )

|
|

12-07-07, 21:03
|
|
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.
|

12-07-07, 22:23
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
|
|
|
|

12-07-07, 22:55
|
|
Registered User
|
|
Join Date: Nov 2003
Location: Sinapore
Posts: 187
|
|
Quote:
|
Originally Posted by r937
use two queries
|
Ok thanks 
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|