MySQL error 1248, Creating proper query. Beginner Q

Go To StackoverFlow.com

1

I am running into some difficulty with the following query.

SELECT maker, speed 
FROM  
(
    SELECT * 
    FROM product 
    NATURAL JOIN laptop
) 
WHERE hd > 30;

I am trying to find the maker and speed of laptops with hard drives greater than 30 gigabytes.

I have two relations laptops and products. Laptops has the tuples (maker, model, type) and laptop has the tuples (model, speed, ram, hd, screen, and price).

What I think I am doing.

  • Joining product with laptop with natural join, which I think and does (when submitted by itself) give me a relation of laptop but with two more columns maker and type.
  • I then am trying to just select maker and speed from that table where the size of the hd is greater than 30.
2012-04-03 19:54
by NoName
Are you receiving an error? If so, please show us the error. Are you receiving a resultset other than what you're expecting? If so, please show us - Bob Kaufman 2012-04-03 19:56
What is the difficulty? Does your query give an error? What database are you using - Andomar 2012-04-03 19:57


3

A subquery in the FROM clause requires a table alias:

SELECT maker, speed 
FROM  
(
    SELECT * 
    FROM product 
    NATURAL JOIN laptop
  /* include an alias with AS */
) AS products_sub
WHERE hd > 30;

From the docs:

A table_subquery is also known as a subquery in the FROM clause. Such subqueries must include an alias to give the subquery result a table name. A trivial example follows; see also Section 12.2.9.8, “Subqueries in the FROM Clause”.

However for your example, the subquery isn't needed at all.

SELECT maker, speed 
FROM products NATURAL JOIN laptop
WHERE hd > 30;

Note that NATURAL JOINs are not usually recommended, and it is best to be explicit about the columns joined in an ON clause.

2012-04-03 19:57
by Michael Berkowski
The subquery serves no puprose and can be omitte - Andomar 2012-04-03 19:57
@Andomar already updated.. - Michael Berkowski 2012-04-03 19:59
Thank you Micheal. That is exactly what was needed - NoName 2012-04-03 20:01
+1 for "natural joins are not usually recommended". Very good point - a_horse_with_no_name 2012-04-03 20:04
@BumSkeeter You're welcome, and welcome to Stack Overflow - Michael Berkowski 2012-04-03 20:10


0

 SELECT maker, speed 
  FROM products   JOIN laptop 
  WHERE hd > 30;
2012-04-04 06:07
by Ankit Sharma