Insert data into table and use that data to create view - mysql

When there is request from user i get parameter x,y,flag and sent. I insert it into table2.
$sql1 = "INSERT INTO table2
(id,sent,pcount,ncount,flag)
VALUES
('".$_POST['id']."','".$_POST['sent']."',' $x ','$y','$flag')";
When data is inserted, below that I am showing chart, which takes inserted values x and y to show the chart.
So how on button click event I can get the values inserted in last insert operation??
Is it possible that I can create view with last row x and y values from table table2.
Table table2 has auto increment column.

CREATE VIEW view_name AS
SELECT xcolumn,ycolumn
FROM table2 WHERE id=SELECT MAX(id)
Assuming you have an incrementing column called id.

If you want to get data from the last insert, use last_insert_id(). This will give you the last inserted id for your connection.
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id

Related

SQL Query select value if exist or create and pass

I want to achieve the following in single SQL Query.
I am inserting values into table "A" for the columns "A1,A2".
I am passing the value for "A1" column directly.
For the column "A2", I need to select the "B2" column from "B" table and pass the value to "A2".
Incase if the "B" table does not contains the value for the condition which i am checking in my query, then i need to insert new row in "B" table and pass the B2 value to "A2".
My above query works well if the B table has value and condition matches.
I am not able to make it when the B table is not having/matching the value. Need help to insert new row and pass the newly inserted row to table A.
INSERT INTO A(A1,A2) VALUES('A1 Value',(select B2 from B where action = 'test'))
You can't do this in one query, since you can only insert into one table at a time. So there's no way to insert a new row into B in the same query if the row is missing.
Assuming action has a unique index, you can do it in 2 queries like this:
INSERT IGNORE INTO B (action, B2) VALUES ('test', 'default value');
INSERT INTO A (A1, A2)
SELECT 'A1 Value', B2
FROM B
WHERE action = 'test';
INSERT IGNORE will do nothing if action = 'test' already exists.

get the last ID after insert in mysql

how can i get the ID of the last insert statement
im using trigger to create a ID for every record
INSERT INTO table1_seqdocument VALUES (NULL);
SET NEW.tracknum = CONCAT('DOC', LPAD(LAST_INSERT_ID(), 3, '0'));
and i need that ID for other table
this is this my insert statement
INSERT INTO tble_transaction
(
tracknum
,signatoryid
,signed
,status
,signatorylevel
)
VALUES
(?,?,?,?,? )
what i want is to retrieve the ID and use it for another insert statement but using other table. is it possible? thank you
You can use ##identity
SELECT ##IDENTITY AS [##IDENTITY];
LAST_INSERT_ID() can only tell you the ID of the most recently auto-generated ID for that entire database connection, not for each individual table, which is also why the query should only read SELECT LAST_INSERT_ID() - without specifying a table. As soon as you fire off another INSERT query on that connection, it gets overwritten. If you want the generated ID when you insert to some table, you must run SELECT LAST_INSERT_ID() immediately after doing that (or use some API function which does this for you).
If you want the newest ID currently in an arbitrary table, you have to do a SELECT MAX(id) on that table, where id is the name of your ID column. However, this is not necessarily the most recently generated ID, in case that row has been deleted, nor is it necessarily one generated from your connection, in case another connection manages to perform an INSERT between your own INSERT and your selection of the ID.
(For the record, your query actually returns N rows containing the most recently generated ID on that database connection, where N is the number of rows in table1.)

How to copy multiple row in table while inserting additional value

How can i copy table and insert new value at the same time
I want to copy a table with multiple rows and insert a same value "Borrowed" for one column Transaction. I already know how to copy but i dont know how to copy and insert new/another value at the same time.
here is what i got:
INSERT INTO TRANSACTION(UserID,TRANSACTION,First_Name,Last_Name,ISBN,Title,DATE)
VALUES (1,"Borrowed",(SELECT First_Name,Last_Name,ISBN,Title),NOW());
Add your fixed values into the SELECT list:
INSERT INTO TRANSACTION(UserID,TRANSACTION,First_Name,Last_Name,ISBN,Title,DATE)
SELECT 1, "Borrowed", First_Name,Last_Name,ISBN,Title,NOW()
FROM <your table goes here>

Can you combine INSERT INTO and Last_Insert_ID in one SQL Statement

I'm need to get the ID of the last record inserted into my table.
INSERT INTO mytable (Column1, Column2) VALUE ('Test', 'Bob');
SELECT/SET LAST_INSERT_ID() as NewID;
Response.Write rst("NewID") '(for example)
Can it run in one statement, or do I need to run the SELECT LAST_INSERT_ID after the INSERT SQL has ran.
I am using MYSQL and ASP
You need to run it after every statment as it will always returns one id of most recent inserted record.
To achieve your goal,
You need to do is, whenever any record inserted to table, get the last id and store it in to any string/table every time and return it from your sproc. This way you get all inserted id at once.
I think that you need to run 2 procedure, first insert and then select order by id desc

Trouble with MAX(col)+1 INSERT into same MySQL table

I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause
You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.