nick
nick

Reputation: 2783

mysql is this query possible?

I need to write the following query so the "att" field is always a constant.

INSERT INTO index(id,val,att) (SELECT ID,val FROM product), 14;

"14" is a constant

how would i write it, if even possible?

Upvotes: 0

Views: 66

Answers (7)

user1285324
user1285324

Reputation:

INSERT INTO table_name(id,val,att) SELECT ID,val,14 FROM product

Dont use table name like index and there is no need for () in select from table.

Upvotes: 0

xpapad
xpapad

Reputation: 4456

INSERT INTO `index`(id,val,att) SELECT id,val,14 FROM product

Index is not a very good name for a table btw... You can use backquotes (`) to escape it, but better pick up another name.

Upvotes: 4

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

INSERT INTO `Index`(id, val, att)
SELECT ID, val, 14 
FROM Product

Upvotes: 1

Ilion
Ilion

Reputation: 6872

You can simply select a number and it will be returned. This should work:

INSERT INTO `index`(id,val,att) SELECT ID,val,14 FROM product;

Notes:

  1. You do NOT need the values keyword for INSERT .. SELECT statements. http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
  2. As another person mentioned, index is not a great table name due to it being a reserved word.

Upvotes: 1

Janaki
Janaki

Reputation: 195

INSERT INTO index(id,val,att) VALUES (SELECT id,val,14 FROM product)

Try it

Upvotes: 0

nico
nico

Reputation: 51640

Just add the constant in the SELECT statement

INSERT INTO `index`(id, val, att) (SELECT ID, val, 14 FROM product);

Upvotes: 2

Marco
Marco

Reputation: 57573

You could use this:

INSERT INTO `index` (id,val,att) 
SELECT ID,val,14 FROM product

Upvotes: 1

Related Questions