I'm trying to insert to my table an average of my select query but I am encountering an error
Here is my query:
INSERT INTO tbl_average(student_id, first_avg) VALUES
('100', AVG(SELECT fir_grad FROM tbl_grade
WHERE student_id='100' AND school_year='2015-2016'))
pls help
use INSERT INTO SELECT syntax:
INSERT INTO tbl_average(student_id, first_avg)
SELECT 100, AVG(fir_grad)
FROM tbl_grade
WHERE student_id=100 AND school_year='2015-2016'
Related
I have a exercise_logs table with these columns:
user_id | exercise_name | mood_log_id | weight_log_id | stretch_log_id
I have three tables (mood_logs, weight_logs and stretch_logs) whose primary key is stored in the 3 last three respective columns in exercise_logs.
These are columns my mood_logs table:
id | mood_scale
So I want to create a new mood_logs entry and use the id from that and store that in exercise_log. But I want to do it all in one query.
Here's what I tried so far:
INSERT INTO `exercise_logs` (`exercise_name`, `user_id`, `mood_log_id`)
values('Breathing', 190, (INSERT INTO `mood_logs` (`mood_scale`)
values(9); LAST_INSERT_ID()))
But this does not work.
Is it possible to achieve this in one query?
Thank you so much.
You can try this
First query insert into mood_logs
INSERT INTO `mood_logs` (`mood_scale`)
values(9);
2nd Query insert into exercise_logs
INSERT INTO `exercise_logs` (`exercise_name`, `user_id`, `mood_log_id`)
values('Breathing', 190, (SELECT `mood_scale` FROM `mood_logs` WHERE `mood_scale` = 9))
You first need to INSERT a row into mood_logs.
As the latest inserted id is from the above statement.
You can use last_insert_id() directly within the second insert statement.
INSERT INTO `mood_logs` (`mood_scale`) values (9);
INSERT INTO `exercise_logs` (`exercise_name`, `user_id`, `mood_log_id`) values('Breathing', 190, last_insert_id());
This is assuming you're the only using the database. If someone else uses an INSERT statement before you can use the last_insert_id() then, the id MIGHT be equal to whatever id the latest insert statement.
Can also use variables to store the last_insert_id();
INSERT INTO `mood_logs` (`mood_scale`) values (9);
SET #arbitraryVariableName = last_insert_id();
INSERT INTO `exercise_logs` (`exercise_name`, `user_id`, `mood_log_id`) values('Breathing', 190, #arbitraryVariableName);
Please help me to insert simple sql query into table for example:
insert into table0 ( sql_text) values ( 'select count(*) from table1;');
I am receiving error .
Thanks,
Please try the following:
INSERT INTO tableName (columnName) values ('Select count(*) from tableName; ');
I am using Hive version 0.13.1. While trying to insert data into an existing table getting an error while using the below query:
CREATE TABLE table1 (order_num int, payment_type varchar(20), category varchar(20));
INSERT INTO TABLE table1 VALUES (151, 'cash', 'lunch');
ERROR :
ParseException line 1:25 cannot recognize input near 'VALUES' '('
'151' in select clause
While searching, got everyone suggesting above query, but unfortunately it's not working for me. Is it due to different Hive version? I am getting this ambiguity due to link here Needs help to insert data to an existing table in Hive.
An insert values statement isn't available in Hive until version .14, so you will need to change your syntax to do a insert select statement.
INSERT INTO TABLE table1 SELECT 151, 'cash', 'lunch';
If you want to insert multiple values then you can union selects
INSERT INTO TABLE table1
SELECT 151, 'cash', 'lunch'
union all
SELECT 152, 'money', 'dinner';
It could be obvious but since I don't have any experience I cannot figure out how to do. Here is the question; say I have table2 with fields id,name, time, price and explanation.Also, there is an other table1 which has name, time and price. I want to insert data from table1 if price is higher than some threshold, while doing so as an explanation I want "The price is above threshold". Can anyone suggest me a way to do so? Thanks in advance.
Try this...
INSERT INTO `table2` (`name`, `time`, `price`, `explanation`)
SELECT `name`, `time`, `price`, 'The price is above threshold'
FROM `table1`
WHERE `price` > 100
INSERT table1 (name, time, price)
SELECT name, time, price
FROM table2
WHERE price > 120
You use an INSERT statement and list the fields you wish to insert into, followed by the SELECT statement you are going to use to populate the fields with. Here's a link: http://dev.mysql.com/doc/refman/5.5/en/insert.html.
Example:
INSERT INTO insert_tablename (field1, field2, field3)
SELECT field1, field2, field3
< The rest of your select statement >
NSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
SELECT ...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
The following conditions hold for a INSERT ... SELECT statements:
http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
In SQL how would I insert into a MAX count of another column
So I have this
INSERT INTO USER(id,name,employee_code,email) VALUES (3,john,(SELECT MAX(employee_code)+1 FROM USER),"john#example.com");
However this doesn't work with the syntax... basically it's like an autoincrement that I have to self build because the employee_code sometimes equals 0 for temporary employees so I need a command to take the max code in their and add one.
You probably forgot quotes around john:
INSERT INTO USER(id,name,employee_code,email)
VALUES (3, 'john', (SELECT MAX(employee_code)+1 FROM `USER`), "john#example.com");
Do away with the VALUES statement and use a select instead:
INSERT INTO USER(id,name,employee_code,email)
SELECT 3, 'john', MAX(employee_code)+1, "john#example.com"
FROM USER
You need to use an INSERT INTO... SELECT ...FROM query:
INSERT INTO USER(id,name,employee_code,email)
SELECT 3, 'john', MAX(employee_code)+1, 'john#example.com'
FROM `USER`;
INSERT INTO USER(id,name,employee_code,email)
SELECT
3,
'john',
MAX(employee_code)+1,
'john#example.com'
FROM USER