As I know I can't use a where clause inside a Insert statement but in here what I want to do is I want to insert a new row if same kind of row not exist else I need to update that row. so I need to use where clause to do that. this is my query but it is not working because of where clause. can someone help me on this.
Thank You.
"INSERT INTO "
. "`leaves_count` "
. "(`leave_cat_id`, `leave_days_count`, `emp_number`) "
. "VALUES ('$req_lv_cat', $req_lv_day_count, '$empNmbr') "
. "ON DUPLICATE KEY UPDATE "
. "leave_days_count = leave_days_count + ($req_lv_day_count) "
. "WHERE "
. "(leave_cat_id = '$req_lv_cat' AND emp_number = '$empNmbr')"
Please use merge statement. It will do "If exist UPDATE else INSERT".
MERGE <hint> INTO <table_name>
USING <table_view_or_query>
ON (<condition>)
WHEN MATCHED THEN <update_clause>
WHEN NOT MATCHED THEN <insert_clause>;
Here i am posting example i hope it will help to you
INSERT INTO table (id,a,b,c,d,e,f,g)
VALUES (1,2,3,4,5,6,7,8)
ON DUPLICATE KEY
UPDATE a=a, b=b, c=c, d=d, e=e, f=f, g=g;
Related
I have a terrible to identify unique record(value).
I have a table like this:
ID NAME DESCRIPTION
1 Yanagida Fumit best author
2 Ha Il-kwan new author
3 Fumit Yanagida best author
4 Ha Il Kwan new author
5 Ilkwan Ha new author
There are 5 records in same table called autho table. But in actually, there are 2 authors.
First record and third record are stored from one author information and second, 4th and 5th are one author.
I want to make this like below.
ID NAME DESCRIPTION
1 Yanagida Fumit best author
2 Ha Il Kwan new author
It means that, I am going to erase all duplicates against reverse name problem.
I wonder if I can compare two values(string) in same column.
Help me please. I will be happy with your any help!
Here is a php solution:
remove_duplicates("Yanagida Fumit");
function remove_duplicates($full_search_str) {
// establish connection to your db
// fetch data
$query = " SELECT `id`, `name`
FROM `" . $tbl_name . "`
WHERE MATCH (`name`) AGAINST ('" . $full_search_str . "' IN BOOLEAN MODE)
AND `name` <> '" . $full_search_str . "'
";
// run query
$results = $conn->query($query);
// loop through results
foreach($results as $result) {
// build query
$query = " DELETE
FROM `" . $tbl_name . "`
WHERE `id` = " . $result['id'];
// run query
$result = $conn->query($query);
}
}
Oracle answer:
create or replace table authors as
Select distinct name, description
from authors;
I am trying to follow the following blog and use an UPDATE, IF, INSERT INTO statement seeing that it will no go over my data twice.
Please note it is for php.
The statement that I have is as follows
$query = "UPDATE
" . $this->table_name2 . "
SET
batch = :batch,
created = :created
WHERE
id = :id
IF row_count() = 0
INSERT INTO " . $this->table_name2 . "
SET
id=:id,
batch=:batch,
created=:created";
But my return value always comes back as false, and I do not know where the problem is.
If I try the first half of the statement it updates the information:
$query = "UPDATE
" . $this->table_name2 . "
SET
batch = :batch,
created = :created
WHERE
id = :id
And if I try the second half of the statement it INSERTS the information:
INSERT INTO " . $this->table_name2 . "
SET
id=:id,
batch=:batch,
created=:created";
I do not find any help after allot of searching so far, and I feel that somehow my IF statement may not be correct, but I do not even get info on the IF row_count() = 0 in the docs.
The IF statement is available in stored procedures only.
If id is the primary key (or unique) this should work for you:
INSERT INTO " . $this->table_name2 . "
SET
id = :id,
batch = :batch,
created = :created
ON DUPLICATE KEY UPDATE
batch = :batch,
created = :created
This will execute the INSERT statement only, of the ID does not exits yet. If it already exists, it will execute the UPDATE part.
I have a mysql update here and when the user make changes to one selection category it would duplicate that selection to the rest of the other selection too. Below is a sample scenario where only the 1st CD category was change and it's reflecting the same for the rest. How do I get "nmc_category.catDesc ='$pCDCategory" to only update that entry only ?
Sample:
$sql = "UPDATE nmc_cd "
. "JOIN nmc_category ON (nmc_cd.catID = nmc_category.catID) "
. "JOIN nmc_publisher ON (nmc_cd.pubID = nmc_publisher.pubID) "
. "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_category.catDesc ='$pCDCategory', nmc_publisher.pubName = '$pCDPubName' , nmc_cd.pubID ='$pCDPubID', nmc_publisher.pubID='$pCDPubID' "
. "WHERE nmc_cd.CDID='$pCDID'";
nmc_cd table:
nmc_category table:
use this command
SET SQL_SAFE_UPDATES=0;
I want to insert two column values(Year + ID(Primarykey-AutoIn) = SRF) in to one column
i can able to concatenate the values using the following code SELECT CONCAT(Year, ID) AS SRF FROM billing_details but i dont know how to insert those values in a column namely "SRF".
Updated Question:
Now i have used the following code
$sqlselect = "UPDATE billing_details SET SRF = CONCAT(Year, ID)";
it returns as "Null"
UPDATE billing_details SET SRF = CONCAT(Year, ID)
You first need to add the column to your table, assuming it does not exist yet:
ALTER TABLE billing_details ADD COLUMN srf VARCHAR(100);
And then use an update statement:
UPDATE billing_details
SET srf = CONCAT(year, id);
The issue has been resolved by using the following code courtesy to Mr.Girish (Column concatenation returns "Null" Mysql - Php) and other sweet supporters(Akond, Mureinik, Jens, Valentin Rusk and etc "Thank you all for giving valuable support" )
//Concatenation of Year+ID=SRF
$sqlselect = "UPDATE billing_details SET SRF = CONCAT(`Year`, `ID`)";
if (!mysqli_query($con,$sqlselect)) {
die('Error: ' . mysqli_error($con));
}
I'm trying to add a new mysql column in a table, using an insert_id from an insert of another table. This is the sentence that i use...
string sqlInsert = "INSERT INTO test (IdPico, Nombre, TextoBienvenida, FechaCreacion) VALUES (1, 'nombretest', 'aslkñdfa lsñdk asjd asldkf añlsj f', '2011-07-13 10:22:53'); ";
sqlInsert += "SET #IDTESTCREATED := CONCAT('Test', LAST_INSERT_ID(); ";
sqlInsert += "ALTER TABLE Usuarios ADD COLUMN #IDTESTCREATED BIT DEFAULT 0; ";
I using ASP.NET 4.0 and MySql connection, and server responds with 'Fatal error encountered during command execution. '
Could anybody help me?
Well ... I answer myself.
After making a deep search, I have not found how to add a column dynamically by a variable in mysql.
At end I had to make two querys, first to insert the test and get the id, and second to update the users table.
Since the insertion and retrieval of id are in the same query, no problems of persistent connections and concurrent updates.
string sqlInsert = "INSERT INTO Test (<fields>) VALUES (<values>);";
sqlInsert += "SELECT LAST_INSERT_ID() AS IdTestInserted; ";
string idnewtest = <result of insert query>;
string sqlAlter = "ALTER TABLE Users ADD COLUMN Test" + idnewtest + " BIT DEFAULT 0; ";
I regret not having found the answer, but at least I achieved my goal.
Thank you all for your help!