I am trying to insert my values into table if Admin_User_Role_Id value against Admin_Id is not present in the table. Is it possible to insert!
My Table Structure:
Admin_User_Id (FK)
Admin_User_Role_Id (FK)
Is_Enabled (boolean flag)
Query which I tried, but not success
INSERT INTO role_association
SET Admin_User_Id=61, Admin_User_Role_Id=2, Is_Enabled=0
WHERE Admin_User_Role_Id
NOT IN
(SELECT Admin_User_Id, Admin_User_Role_Id FROM role_association)
I think it is possible but my logic is wrong. How should I manage this query to work successfully!
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.
Probably you take care about where condition in your programming logic and write a simple insert statement and the depending on your logic use this statement to insert the records. Hope you got my point.
You want to insert your values in your table using this query for your reference
INSERT INTO Yourtablename(column1,column2,column3,...)
VALUES ('value1','value2','value3',.....);
You want to ADD one new column in your table means using this query
** ALTER TABLE table_name ADD column_name datatype**
Related
I have the following problem:
I would't insert all the values in my db.
So, i would insert only id,tessera,nome,sex. I thought i had to write something like this:
INSERT INTO registries(id,tessera,'',nome,sex) VALUES (35983,35983,'DOG', 'FABIO', 'M')
I don't want to insert the DOG value in my db cause I don't have any attribute in my schema to save it.
I have 3000 rows and I take this row as example so I can't simply delete the DOG value and the attribute field in my sql query.
I remember that I have to use something like this
INSERT INTO registries(id,tessera,'',nome,sex)
I mean, I have to write '' to not consider the value in that position.
but MySQL gives me an error..
Can you help me?
You can handle this situation in following ways.
create temporary table on same server. Insert all data in temporary table then use insert into select statement and select required fields.
create temporary column and insert data in that column and delete temporary column
INSERT INTO registries (id,tessera,new_column,nome,sex)
VALUES (35983, 35983, '', 'FABIO', 'M');
Now
DROP COLUMN new_column from registries;
Try this (assuming dog is a column name):
INSERT INTO registries (id,tessera,dog_name,nome,sex) VALUES (35983, 35983, '', 'FABIO', 'M')
If dog_name is allowed to be NULL (which it is by default) you could also just not insert any value, like this:
INSERT INTO registries (id,tessera,nome,sex) VALUES (35983, 35983, 'FABIO', 'M')
My table Structure,Table Name- user_tb
User_Id Name Mob_No City
=================================
100 Sumith 34542 dfsc
101 Yadhu 35485 dfgd
102 Aby 34234 jhhg
Here column User_Id is auto_increment
and am inserting values to this table using this query,
insert into user_tb(Name,Mob_No,City) values('Yadhu',34542,'dfsc');
I need to get User_Id of the Person at the time of insertion
ie, when am inserting using this query,
insert into user_tb(Name,Mob_No,City) values('Yadhu',34542,'dfsc');
i need to get User_id-101
Is this possible....
Anyone please help me to complete this.
Obtaining the value of column that uses AUTO_INCREMENT after an INSERT statement can be achieved in a number of different ways. To obtain the value immediately after an INSERT, use a SELECT query with the LAST_INSERT_ID() function.
For example, using Connector/ODBC you would execute two separate statements, the INSERT statement and the SELECT query to obtain the auto-increment value.
insert into user_tb(Name,Mob_No,City) values('Yadhu',34542,'dfsc');
SELECT LAST_INSERT_ID();
If you do not require the value within your application, but do require the value as part of another INSERT, the entire process can be handled by executing the following statements:
insert into user_tb(Name,Mob_No,City) values ('Yadhu',34542,'dfsc');
insert into tbl2 (id,text) VALUES (LAST_INSERT_ID(),'text');
In Mysql: use LAST_INSERT_ID() function; http://dev.mysql.com/doc/refman/5.0/en/mysql-insert-id.html
If PHP: use mysql_insert_id() to get the last id inserted by your query.
http://php.net/manual/en/function.mysql-insert-id.php
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.
I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image.
I need to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate my string in the query statement.
Thanks in advance.
I would keep the id and imgname independent of each other and combine the two on SELECT when needed. If the need is frequent enough, create a view.
Have a look at LAST_INSERT_ID() function. If performance is not an issue, INSERT regularly, and then UPDATE using LAST_INSERT_ID(), like:
UPDATE table SET name = CONCAT(name, "-", LAST_INSERT_ID()) WHERE id = LAST_INSERT_ID();
I would like to insert a record into a table if a record doesnt exist already exist with that domain name. The following SQL should achieve this but is getting an error.
The reason I want to do the update first is because I am doing multiple updates later in my code and need the record in my table first before doing all of the updates.
Why am I getting an error on this mySQL query?
insert into domain (name)
values ('domain.com.au')
WHERE NOT EXISTS
(
select name
from domain
where name = 'domain.com.au'
);
Both queries when split work fine but when together do not.
Let your database handle it for you. Use a unique index on name and your INSERT will fail if you try to insert a duplicate.
CREATE UNIQUE INDEX idx_name ON domain (name)
You cannot combine WHERE with INSERT clause. Use REPLACE INTO instead.
What error are you getting?
My guess would be the that the select inside the 'where not exists' is not allowed.