This question already has answers here:
Get Updated Value in MySQL instead of affected rows
(5 answers)
Closed 9 years ago.
$query = "UPDATE transaction SET c_status = :status WHERE c_name = :name AND c_id = :id";
$stmt = $this->handle->prepare($query);
$stmt->bindParam(':c_status',$status,PDO::PARAM_STR);
$stmt->bindParam(':c_name',$name,PDO::PARAM_STR);
$stmt->bindParam(':c_id',$id,PDO::PARAM_STR);
return $stmt->execute();
Using the above syntax, I am able to update a record in the transaction table. However, what I only get with the return is a boolean. I want to know if there is a way I can get the transaction_id (the AUTO_INCREMENT field in the transaction table, c_id and c_name where just a column of that) and the rest of its columns?
This question is not related to PDO but to mysql in general.
UPDATE queries are not intended to return anything. To get a row from database you have to use SELECT query.
Related
This question already has answers here:
How to make MySQL table primary key auto increment with some prefix
(4 answers)
Autoincrement ID with a prefix
(3 answers)
Closed 5 years ago.
ID column set to primary key, auto increment.
I want to have a second column Project number, is it possible to set something like this in SQL ? Or how should I do this ?
ID: 1 projectnumber:Project001
ID: 2 projectnumber:Project002
.
.
.
ID: n projectnumber: Project00n
you could do that in 2 ways:
Either using a trigger or do that when retrieving the record from the database:
trigger: after insert
CREATE TRIGGER `yourtable_after_insert` AFTER INSERT ON `yourtable` FOR EACH ROW BEGIN
UPDATE yourtable
SET projectnumber = CONCAT('project', NEW.id)
WHERE id = NEW.id;
END;
or
just do that CONCAT thing in your select query or even better in the logic of php. Consider the possibility you want to translate your application. You would store duplicate information as well...
as pointed out below: NEW.id will not work.
So use LAST_INSERT_ID() instead:
CREATE TRIGGER `yourtable_after_insert` AFTER INSERT ON `yourtable`
FOR EACH ROW BEGIN
UPDATE yourtable
SET projectnumber = CONCAT('project', LAST_INSERT_ID())
WHERE id = LAST_INSERT_ID();
END;
but still: it would be duplicating content
This question already has answers here:
Concatenate string with field value in MySQL [duplicate]
(4 answers)
Closed 6 years ago.
I know this can update a table column to a new value from the previous value UPDATE table SET column=column+2 but this only happen to an integer value, I want to update a text value like this
// if the table column value is = "james,john,peter"
$new = ",andrew";
mysqli_query($con, "UPDATE table SET column=column+'".$new."');
so the new table column value will now be james,john,peter,andrew. I have searched even stackoverflow but all answers I got is for integer value. Please anyone with an idea?
Use the concat() function:
UPDATE table set column=concat(column, ',andrew');
This question already has answers here:
With MySQL, how can I generate a column containing the record index in a table?
(8 answers)
Closed 8 years ago.
I have: Select some set of pairs. The first column is the id of row in table, the second is the new value which should be assigned to that row.
-- the first query
CREATE TABLE tmp;
SELECT row_id, new_value
FROM [not essential tables and joins]; //PSEUDOCODE
-- another queries
FOR EACH tmp //PSEUDOCODE
UPDATE table SET value = new_value WHERE id = row_id;
-- QUESTION: CAN I MERGE SELECT AND UPDATE IN ONE QUERY?
-- I want avoid creating temporary table.
Problem: Iteration through table (as in example above) decrease clearness and speed of code.
Question: *How to do the same in single query
I think you are looking for update the table join with other table (Not sure though). You can do something like
UPDATE tmp a
JOIN sometable b ON a.col = b.col
AND a.id = b.row_id
SET a.value = b.new_value
This question already has answers here:
How to get the next auto-increment id in mysql
(21 answers)
Closed 9 years ago.
I am using MySQL.
I want to retrieve the next value that the AUTO_INCREMENT column will take without entering a new record.
create table ABC(id int(10) NOT NULL AUTO_INCREMENT,name char(10));
In oracle I would have used sequencename.nextval(); But what to I use in MySQL?
Here is why I did not use
select max(id) from ABC;
Suppose I have an entry with id=2. Now column id will take the next value as 3.
Before I create a record with id=3, If I delete the record with id=2.
The answer for query I mentioned will be 2. But I want the actual value 3, which the auto_increment column will anyway take.
Query table status like this:
SHOW TABLE STATUS WHERE `Name` = 'table_name'
Now in result you will get a column named Auto_increment. This is the value You were asking for.
In JAVA:
conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword);
stmt = conn.createStatement();
rs = stmt.executeQuery("SHOW TABLE STATUS WHERE `Name` = 'table_name'");
rs.next();
String nextid = rs.getString("Auto_increment");
Full example here: http://www.avajava.com/tutorials/lessons/how-do-i-use-jdbc-to-query-a-mysql-database.html
If I understand correctly,you could use the number of rows as indicator:
SELECT TABLE_ROWS+1
FROM information_schema.tables
WHERE table_name='tableName'
AND table_schema = DATABASE();
There is no way to guarantee what value you are going to get before inserting the row. This is mostly because you will have to lock the entire table to guarantee no other thread will do an insert with "your" next value.
You can reserve a value by starting a transaction, inserting a row, getting the value and then doing a rollback. Then you can safely use that value.
It will be much simpler to just insert the row, so maybe I'm not understanding the purpose of what you are doing.
This question already has an answer here:
Syntax error on MERGE statement
(1 answer)
Closed 9 years ago.
Here is my current statement
INSERT INTO TABLE (LAST_NAME, FIRST_NAME, DOB, ACCESSION, EXAM_DESC, LOCATION, EXAM_DATE, REFERRING)
VALUES (${patientIdentification_patientName_familyName},${patientIdentification_patientName_givenName},${patientIdentification_dateOrTimeOfBirth_value},${commonOrder_placerOrderNumber_entityIdentifier},${observationRequest_relevantClinicalInfo_value},${patientVisit_assignedPatientLocation_facility},${observationRequest_observationDateOrTime_value},${observationRequest_orderingProvider_familyName})
ACCESSION is unique in the db. What I need to do is if the ACCESSION value already exists then I want to replace the row. If it does not exist (the value of ACCESSION is unique) I want it to INSERT.
I assume from your variables that you are using php to do this. Correct me if i am wrong.
This is how you would check if it exists:
$result = mysql_query("SELECT * FROM DBTable WHERE ACCESSION = '$ACCESSION'");
$row = mysql_fetch_array($result);
if($row > 0)
{
//code for when the record exists (update)
}
else
{
//code for when the record does not exist (insert)
}
Follow this and modify it for your dbtable and values etc.
This should do what u want it to do