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
Related
As I know that many single insert statement is so slow. It can be better by handling like below.
insert into table1 values(c1,c2,c3) values(c4,c5,c6).
I'm interested in how insert into ... select ... execute in MySQL. Are there any optimizations in it or Innodb? At last, my English is not so good.Sorry - -||.
I think you are looking for this
INSERT INTO mytable (name, address, age)
VALUES('raj', 'jaipur', '15'),
('abc', 'india', '42'),
('xyz', 'jaipur ', '29'),
('taqi', 'Damsna', '32');
I am new to SQL and relational databases, but am writing the code to add data into tables I have created. I am not sure if the code I have written is correct for inserting into a table. Can anyone tell me if this is correct please.
INSERT INTO Job (Job_ID, Job_name)
VALUES (‘1’, ‘Teacher’);
You code is right but if you use autoincrement column for job_id you should use
and use single quote s and not ‘
INSERT INTO Job ( Job_name)
VALUES ( 'Teacher');
otherwise the code you prodivided
INSERT INTO Job (Job_ID, Job_name)
VALUES ('1', 'Teacher );
You can also try like this :
insert into table_name set column_first = 'value_first' and column_second = 'value_second'
make correction:
INSERT INTO Job (Job_ID, Job_name)
VALUES ('1', 'Teacher');
You can insert value with this syntax
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
As per your code you can write
INSERT INTO Job (Job_ID, Job_name)
VALUES ('1', 'Teacher');
This my help:
w3schools
simple insertion
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
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'
I have a MySQL db table with a column containing strings. And I have a list of strings. Some of the strings from the list can be found in my table, others can't. See my table TableName:
<TableName>
IntegerColumn, StringColumn
1, one
2, two
3, three
4, four
If I execute the query
SELECT * FROM TableName WHERE StringColumn NOT IN ('three', 'four', 'five', 'six');
I get a result of two rows, which contain nothing but NULL.
How can I see for which of the strings there was no match? Because I want to add them to the db table
Thx in advance
Using the following sample
CREATE TABLE sample_table
(
id int auto_increment primary key,
details varchar(30)
);
INSERT INTO sample_table
(id, details)
VALUES
(1, 'One'),
(2, 'Two'),
(3, 'Three'),
(4, 'Four'),
(5, 'Five');
I ran the query
SELECT * FROM sample_table
WHERE details NOT IN ('two', 'three', 'nine');
which gave the correct output of:
1 One
4 Four
5 Five
If you've got NULL returned then there is something you're not explaining in your question. Can you provide schema information or even a SQL Fiddle and I'm sure you'll get a much better answer.
I think what you want is, 'three', 'four', 'five', 'six' if any of this string is not present in the database you want to identify that.
Using query I think it will be tough. You can just use below query to get the available strings and the counts. Then, if you are using a programming language you can identify which string are not present in the result and then proceed further.
SELECT StringColumn, count(*) FROM TableName group by StringColumn
Not sure if this is what you are looking for.
This should not go this way. Check the following demo to ensure that your code is correct. Now what really matters is the set of data present in the table.
DEMO
Here is the DDL:
create table tab1 (IntegerColumn int(2), StringColumn varchar(20));
insert into tab1 values(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four');
I have to insert big amount of records in a table. It is not quite normalized, so most of the fields are repeated.
I know the proper command is:
INSERT INTO table_name (field1, field2, ..., field_n)
VALUES (value1, value2, ..., value_n),
...
(value1, value2, ..., value_n)
But I wonder whether it is possible to keep some of the values fixed and just indicate the different ones.
Let's say instead of
INSERT INTO table_name (shop, month, sale)
VALUES (1, 2, 23),
(1, 2, 28),
(1, 2, 29),
(1, 2, 30)
Having something like
INSERT INTO table_name (shop, month, sale)
VALUES (1, 2, 23), ... 28 / 29 / 30
If it is not possible I would create a procedure with a loop, feeding a string, etc. It would not be a big issue, but my point is to know if INSERT INTO has any particularity that allows doing this without procedures.
You can try something like the following:
INSERT INTO table_name (shop, month, sale)
SELECT * FROM
(SELECT 1 as shop, 2 as month) as sm,
(SELECT 23 as sale UNION ALL SELECT 28 UNION ALL SELECT 30) as sales;
You can use the default constraint which will add the default value when you do not specify that in the insert into statement. If you specify a value that value will be added.
Just set the default value for your column in your table
ALTER TABLE tblname ALTER columnName SET DEFAULT 'value'
Refer http://www.w3schools.com/sql/sql_default.asp
You can use a temporary table to insert the different values and then use insert ... select. I don't know if it will be a big saving for you:
CREATE TEMPORARY TABLE sale_temp (sale int);
INSERT sale_temp (sale) VALUES (23), (28), (29), (30);
INSERT INTO table_name (shop, month, sale)
SELECT 1, 2, sale
FROM sale_temp;
DROP TABLE sale_temp;