MitchellSalad
MitchellSalad

Reputation: 4281

Is it possible to get the integer value of a table in MySQL?

I have a database query

SELECT Id FROM Phone WHERE Model = 'Droid'

that returns a table with one entry (because Id is a primary key). The table looks like this:

+----+
| Id |
+----+
|  1 |
+----+

Is there any way to get at that integer "1"? I'd like to use it in the following query, but it's generating a syntax error:

INSERT INTO Sale (Id, Price)
    VALUES (SELECT Id FROM Phone WHERE Model = 'Droid', 100.00);

Upvotes: 0

Views: 51

Answers (4)

Tim
Tim

Reputation: 7780

You simply need brackets around your select query like so:

INSERT INTO Sale (Id, Price)
VALUES ((SELECT Id FROM Phone WHERE Model = 'Droid'), 100.00);

That should work correctly.

Upvotes: 1

atxdba
atxdba

Reputation: 5216

It's not that you're getting some kind of type incompatibility; your syntax is just wrong. Try simply

INSERT INTO Sale (Id, Price)
(SELECT Id,100 FROM Phone WHERE Model = 'Droid');

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230561

Use this:

INSERT INTO Sale (Id, Price)
SELECT Id, 100.00 
  FROM Phone 
  WHERE Model = 'Droid';

Upvotes: 3

gahooa
gahooa

Reputation: 137592

Subqueries need wrapped in their own ()

Upvotes: 1

Related Questions