I have the following MySQL table named users
+----------------+----------------+----------+ +----------+
| uniquenum | state | type | | custid |
+----------------+----------------+----------+ +----------+
+----------------+----------------+----------+ +----------+
| 00001 | 03 | 1 | | 10300001 |
+----------------+----------------+----------+ +----------+
| 00002 | 02 | 3 | | 30200002 |
+----------------+----------------+----------+ +----------+
The above three columns uniquenum, state and type are all concatenated and shown in the custid column. This I was able to achieve by running the following SQL query:
UPDATE users SET custid = concat (type,state,uniquenum)
What I am trying to achieve is to make the custid column to automatically get the values of the other three columns when new values are added or the old ones updated. So, I tried to create a trigger as follows:
CREATE trigger insert_custid
before insert on users
for each row
set new.custid = concat(new.type,new.state,new.uniquenum);
create trigger update_custid
before update on users
for each row
set new.custid = concat(new.type,new.state,new.uniquenum);
When I do this and insert into the table, the custid column does not store the correct values. Instead it returns all zeros in place of the value of uniquenum.
uniquenum is AUTO_INCREMENT with zerofill and starts at 00001 with an interval of 1. Could this be causing an issue? Any help is greatly appreciated. Thank you :)
Related
How I can add values to my mysql table with php without using update and only using select?
My table have a row with the name sale. How I can do this with php?
Thanks.
#EDIT:
One guy give me this but isn't working for me:
SELECT saldo + 50 as saldo FROM tabela;
Some code would be helpful, but you cannot add values into your MySQL table with using UPDATE, UPDATE is to UPDATE values.
UPDATE tbl SET sale = 10000 WHERE tbl_id=1;
To insert/add values, you have to use INSERT:
INSERT INTO tbl (sale) VALUES (10000);
You can use SELECT only if you are for example, pulling data from one table into your current one:
INSERT INTO tbl (sale) SELECT (sale_scores) FROM tbl2;
Or another way is to use SELECT INTO
SELECT * INTO tbl3 FROM tbl;
You can use REPLACE which has the same syntax as INSERT, but replaces the existing row at the coincidence of a unique key.
Example:
Table before:
| id | name |
| -- | ---- |
| 1 | Nick |
| 2 | John |
Run: REPLACE table_name (id, name) VALUES (2, "Sam")
Table after:
| id | name |
| -- | ---- |
| 1 | Nick |
| 2 | Sam |
Personally, I'd use PDO with a simple UPDATE request and bind the params. I see no point in trying to get around using something that is already in place and works a treat.
Here's an example of what I'd do.
$stmt = $this->conn->prepare("UPDATE `table` SET `saldo` = `saldo` + 50 WHERE `username` = :uname");
$stmt->bindParam(":uname",$username);
$stmt->execute();
I have a table of around 6000 records with a date column amongst other columns which represent the deadline for a query. I need to compare the date in the column to todays date which I understand is done something like:
SELECT DATEDIFF(DATE_TO_COMPARE, CURDATE());
However, I then have another comlumn I want to set to that date difference. So for each date, I need to compare, insert the difference in the column difference_in_days, iterate to the next date and repeat.
I am also invoking this function whenever a certain page on my site is loaded using AJAX and PHP/PDO
My SQL knowledge isn't that extensive, how can I achieve this.
Table is kinda of like
field 1, field2, field 3, date_to_compare, field 4, field 5, difference_in_days
| | | | 2016-04-20 | | | |
| | | | 2016-04-25 | | | |
| | | | 2016-04-22 | | | |
| | | | 2016-04-27 | | | |
| | | | 2016-04-29 | | | |
Sonds like you want to do an update?
UPDATE table_name
SET difference_in_days = DATEDIFF(date_to_compare, CURDATE());
This will update every record in the table to the diff of the current date.
However, this will require you running the update every day, if you want that column to maintain relevance.
Alternative Approach:
If you're not querying this a lot, you may be better off using a view, which will update real-time every time you query it.
CREATE VIEW diff_view_name AS
SELECT *, DATEDIFF(date_to_compare, CURDATE()) AS difference_in_days
FROM table_name;
Then you could query it using:
SELECT * FROM diff_view_name;
I have the following Mysql-table:
+-------+--------+------------+----+-------+
| Name | Number | Department | id | JobID |
+-------+--------+------------+----+-------+
| Sven | 2204 | Marketing | 10 | 111 |
| Peter | 2304 | IT | 20 | 222 |
| Bjorn | 4409 | IT | 30 | 333 |
+-------+--------+------------+----+-------+
I get the three columns: Name, Number, Department from a system where I don't have the id and need to perform something in my php script.
Now I would like to performa an insert if there is a new record. If there is an existing record I would like to perform an update, if something changed like Name, Number or Department.
For example, if Number changes it should compare Name and Department and then change number. If Departmend changes it should compare Name and Number and then change Department and so on.
The problem is, that I can not use insert...on duplicate key, because I don't get the primary key.
If I use Replace Into it deletes me also the entry for JobID. Is there a solution how to perform a sql that it will insert and also update if there is now entry? Or something that can do the trick?
Thanks for your help!
I have a table with 4 fields. Form values are saved in table, i want to concatenate two fields and save in third fields. I need to insert data and at the same time concatenate the values to one column. I need to retrieve the 'id', 'abbr' and save in 3rd column. it will be 'abbr'00'id'. Please somebody help me with the query to run. id is an auto increment field.
id abbr trans_id
----------------------
10 | mm | mm0010
15 | ss | ss0015
20 | ss | ss0020
Its for naming the PDF created by form. When i manually use $pdf-
Output($row['abbr'].'00'.$row['id'].'.pdf','F')
It works fine but when i insert same 'abbr' in form the created PDF get replaced by new one.But mysql keeps the record with same field value. I dont know if there is any other solution..Plz help, developers...
Use this SQL
UPDATE table
SET thirdFieldName = CONCAT(firstFieldName, secondFieldName)
This is for MySQL. It will concat the first field value and the second field and update the third field with the value
For example:
+-----+------+----------+-------------+
| id | abbr | trans_id | forth_field |
+-----+------+----------+-------------+
| 10 | mm | mm0010 | |
+-----+------+----------+-------------+
UPDATE table
SET forth_field = CONCAT(abbr, '00', id)
+-----+------+----------+-------------+
| id | abbr | trans_id | forth_field |
+-----+------+----------+-------------+
| 10 | mm | mm0010 | mm0010 |
+-----+------+----------+-------------+
I wish to update one table in my database, the data is from a php POST. (It is a page where multiple edits on rows can take place at once, then it processes them all at once after) and i want it so for each "row" or "loop", it builds a single query that can update all the rows at once.
What i want to do, is in the query, select data from two other tables.
E.g
Posted data:
- Task = "Check current Sponsors"
- User Assigned = "Dan"
- Start Meeting = "Mar 1st"
- Meetings Required = 2
And for User Assigned, i want it to basically do this query:
SELECT id FROM team WHERE fullname LIKE 'Dan'
And for the start meeting, i want it to do this query:
SELECT id FROM meetings WHERE starttime='".strtotime("Mar
1st")."'
-- strtotime() makes a unix timestamp from a string.
but i want it to do that for each "task" that gets submitted. (It is queued up via javascript and it sends them all into the same post request)
Anyone have any ideas on how to do this?
Thanks in advance
Table Structures:
Tasks:
id | startmid | length | task | uid | completed
1 | 2 | 1 | Check Sponsors | 1 | 0
Meetings: (Joined by startmid)
id | maintask | starttime | endtime
1 | Sponsors | 1330007400 | 1330012800
Team: (Joined by uid)
id | fullname | position | class | hidden
1 | Team | All Members | black | 0
2 | Dan S | Team Manager | green | 0
you can use the following construct:
UPDATE mytable( col1, col2 )
SELECT col1_val, col2_val
FROM someothertables
WHERE cond1 = cond1;