MySQL insert always inserts defaults - mysql

Im trying to run this query
("INSERT INTO units SET id="+toID+" AND num="+number+" AND unit_id="+type+"")
this is the log
[2014-05-13 13:09:51] Running query: INSERT INTO units SET id=3 AND num=10 AND unit_id=1
these values are never inserted, it defaults to 0. It only happens on insert queries, i can select fine.
It inserts fine from a php script, however im using Kumulos KScript to run these queries, all have worked fine apart from this one.
Is there a setting on the table that i have missed or something?
Any suggestions?
Many Thanks,
Paul.

INSERT INTO with SET in MYSQL does not need AND as you have
The correct syntax is
INSERT INTO table
SET
col1 = 'val1',
col2 = 'val2',
col3 = 'val3'
OR You can use the following
INSERT INTO table
( col1, col2, col3 )
VALUES
( 'val1', 'val2', 'val3' )
http://dev.mysql.com/doc/refman/5.6/en/insert.html

How about:
INSERT INTO units ( id, num, unit_id ) VALUES ( toID, number, type )
Or:
INSERT INTO units id=toID, num=number, unit_id=type

It is not a correct INSERT query.
Try this:
("INSERT INTO units(id, num, unit_id) VALUES ("+toID+", "+number+", "+type+")")

Related

max value of no autonincrement column in update query

Is it possible to retrieve the maximum value of a key column, increase it and insert it as a new value in the database, like this (pseudo-sql)
insert into mytable (mykeycolumn,col1,col2)
values((max(mykeycolumn)+1),'val1','val2');
Yes, you INSERT from a SELECT, but your fixed fields must "come from the SELECT" as well, like such:
INSERT INTO mytable (mykeycolumn, col1, col2)
SELECT MAX(mykeycolumn)+1, 'val1', 'val2'
FROM mytable;
Complementing: as a_horse_with_no_name pointed out, MAX() + 1 could cause your problems if you have simultaneous transactions in mytable. At some point two identical mykeycolumn values would be generated and you would get an error. The ideal solution is to convert your table to use auto_increment.
You can with this:
INSERT INTO mytable (mykeycolumn,col1,col2) VALUES
((SELECT MAX(mykeycolumn) FROM mytable AS foo)+1,'val1','val2');
try this
INSERT INTO mytable (mykeycolumn, col1, col2)
SELECT SCOPE_IDENTITY()+1, 'val1', 'val2' FROM mytable;

INSERT ... SELECT(insert many rows)

i want to insert the scholar's id to the tblinbox. Here is my query:
$sql = "INSERT INTO tblinbox VALUES ('','$sender','$type','$subject','$LRN','$content','$date', '$newyearLevel','','$userType','THIS_IS_FOR_THE_ID_OF_THE_SCHOLAR')
SELECT id FROM tblscholar WHERE schoYear = '$newyearLevel'";
my problem is,it is not inserting. what will i change in my query?
INSERT ... SELECT syntax does not allow for VALUES declaration. The values ARE the results returned from the SELECT.
See the documentation here: http://dev.mysql.com/doc/refman/5.6/en/insert-select.html
I honestly am not fully sure what you are trying to do with your insert. If you are trying to insert the same values held in your variables for each id value from the tblscholar table then perhaps you need to do something like this:
INSERT INTO tblinbox
/*
maybe add column definitions here to make it clearer
column definitions could look like this:
(
someField,
type,
subject,
LRN,
content,
`date`,
newyearLevel,
someOtherField,
userType,
id
)
*/
SELECT
'',
'$sender',
'$type',
'$subject',
'$LRN',
'$content',
'$date',
'$newyearLevel',
'',
'$userType',
id
FROM tblscholar
WHERE schoYear = '$newyearLevel'
An INSERT statement supports either a VALUES clause followed by a row of values, or else a SELECT query with columns to match the columns of the table you want to insert into.
But not both!
But you can add constant values into your SELECT query:
$sql = "INSERT INTO tblinbox
SELECT '','$sender','$type','$subject','$LRN','$content','$date',
'$newyearLevel','','$userType', id
FROM tblscholar WHERE schoYear = '$newyearLevel'";
considering id is the first column in your insert statement, try this
$sql = "INSERT INTO tblinbox VALUES ((SELECT id FROM tblscholar WHERE schoYear = '$newyearLevel'),'$sender','$type','$subject','$LRN','$content','$date', '$newyearLevel','','$userType')";
You can insert values either fetching values form another table or providing values as follows:
Way 1:
INSERT INTO tblinbox(coloumn_name1,coloumn_name2) VALUES (value1,value2);
Way 2:
INSERT INTO tblinbox(coloumn_name1,coloumn_name2) SELECT value1,value2 from tblscholer where schoYear= '$newyearLevel';

MYSQL bulkinsert: multi insert with value into a select

I have a question about the multi-ing (bulk) insert with mysql..
I know that:
INSERT INTO "my_table" ('col1','col2','col3') VALUES
(1,1,1),(2,2,2);
But I need to do something like:
INSERT INTO "my_table" ('col1','col2','col3') VALUES
((SELECT select1 as col1,select2 as col3 from "my_table2"),"textForAllCol2")
Where my select return a list of 2 column.
I'M trying to explain the best i can..
Thanks for your help!
JP
Here is referenced SQLFiddle for you
Modify your query as
INSERT INTO my_table SELECT col1, "textForAllCol2", col2 FROM my_table2;
You can use it like this
INSERT INTO "my_table" ('col1','col2','col3')
SELECT select1, "textForAllCol2", select2 from my_table2
try this
INSERT INTO my_table (`col1`,`col2`,`col3`)
SELECT select1, "textForAllCol2", select2 from my_table2
If your source and destination columns names are related (even partially) with each other, I prefer to use aliases for readability:
INSERT INTO my_table (`col1`,`col2`,`col3`)
SELECT `col1`, "textForAllCol2" as `col2`, `select3` as `col3` from my_table2

MySQL direct INSERT INTO with WHERE clause

I tried googling for this issue but only find how to do it using two tables, as follows,
INSERT INTO tbl_member
SELECT Field1,Field2,Field3,...
FROM temp_table
WHERE NOT EXISTS(SELECT *
FROM tbl_member
WHERE (temp_table.Field1=tbl_member.Field1 and
temp_table.Field2=tbl_member.Field2...etc.)
)
This worked for one scenario,But now my interest is to upload data directly from the program itself without using two tables. What i want is to upload the data which is not in the table. The sql i had in my head was like the following,
INSERT INTO tbl_member (SensorIdValue, DataTimeValue, DataInValue, IncompleteValue, SpiValue, InfoValue)
VALUES ('Sensor.org', '20121017150103', 'eth0','','','')
WHERE (SensorIdValue != 'Sensor.org'AND DataTimeValue != '20121017150103'AND DataInValue != 'eth0'AND IncompleteValue != ''AND SpiValue != ''AND InfoValue != '');
But it's wrong.. may i know the proper way of doing it please, Thank you very much :)
INSERT syntax cannot have WHERE clause. The only time you will find INSERT has WHERE clause is when you are using INSERT INTO...SELECT statement.
The first syntax is already correct.
you can use UPDATE command.
UPDATE table_name SET name=#name, email=#email, phone=#phone WHERE client_id=#client_id
INSERT syntax cannot have WHERE but you can use UPDATE.
The syntax is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
If I understand the goal is to insert a new record to a table but if the data is already on the table: skip it! Here is my answer:
INSERT INTO tbl_member
(Field1,Field2,Field3,...)
SELECT a.Field1,a.Field2,a.Field3,...
FROM (SELECT Field1 = [NewValueField1], Field2 = [NewValueField2], Field3 = [NewValueField3], ...) AS a
LEFT JOIN tbl_member AS b
ON a.Field1 = b.Field1
WHERE b.Field1 IS NULL
The record to be inserted is in the new value fields.
Example of how to perform a INSERT INTO SELECT with a WHERE clause.
INSERT INTO #test2 (id) SELECT id FROM #test1 WHERE id > 2
merge into table2 chg
using table1 src on src.id = chg.id
when not matched then
insert (chg.id, chg.desc)
values (src.id, src.desc)
when matched then
update set chg.desc = src.desc;
The INSERT INTO Statement
The INSERT INTO statement is used to insert a new row in a table.
SQL INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two forms.
The first form doesn't specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

INSERT result of a SELECT and other values as well

I want to know if this is possible without a procedure or server side calls into the database.
I am trying to insert values into a table based on a select, and other values that will be provided from the server.
The select statement will return more than one result.
I am aware of the existence of INSERT SELECT, but is there any SELECT INSERT ? or a way to insert based on the results of a select ?
thank you
Not really sure what seems to be the problem.
You can do like this:
INSERT INTO table (columns)
SELECT
column or column expression1,
column or column expression2,
…
constant or constant expression1,
constant or constant expression2,
…
FROM a set of tables/joins
WHERE …
Not necessarily in that order (columns, then constants), no. You can mix columns with constants any way you like, just follow the order of the columns you are inserting into.
Was that what you were asking about?
I don't see why an
INSERT INTO yourtable(col1, col2, col3)
SELECT col1, col2, col3
FROM yourothertable
doesn't work for you. But you could always do a SELECT INTO #temptable to save your query in a temporary table and then you could INSERT that data or manipulate it prior to inserting. This is just a long way around the original idea, though.
Am I misunderstanding your questions?
Yes. Use this query:
INSERT INTO FOO (oof, rab) SELECT (foo, bar) FROM BAR;
I think you can do this:
INSERT INTO targetTable (col1, col2, col3)
SELECT col1, col2, col3 FROM sourceTable
UNION ALL
SELECT 'something' AS col1, 'something else' AS col2, 'yet something else' AS col3 FROM DUAL;