I have created two tables subscription_pending and subscription_complete
subscription_complete contain
id mag1 mag2 mag3 mag4
4 100 0 100 0
subscription_pending contain
id mag1 mag2 mag3 mag4
4 100
I have insert value by following command
$final_q= "INSERT INTO `subscription_complete` (`id`, `mag1`, `mag2`, `mag3`, `mag4`) VALUES ('".$row_q['id']."','".$row_q['mag1']."','".$row_q['mag2']."','".$row_q['mag3']."','".$row_q['mag4']."'
i want to update the subscription_complete table without replacing its value i.e. 100 for that i hvae write following query
$final_q1= "update subscription_complete set mag1='".$row_q['mag1']."',mag2='".$row_q['mag2']."',mag3='".$row_q['mag3']."',mag4='".$row_q['mag4']."' where id='".$row_q['id']."'";
can you tell me how to set 100 value in remaining column where the value is 0 ?
Thank you in advance !!
This assumes the empty fields in subscription_pending contain NULL, not empty strings.
UPDATE subscription_complete AS c
JOIN subscription_pending AS p ON p.id = c.id
SET c.mag1 = IFNULL(p.mag1, c.mag1),
c.mag2 = IFNULL(p.mag2, c.mag2),
c.mag3 = IFNULL(p.mag3, c.mag3),
c.mag4 = IFNULL(p.mag4, c.mag4)
If they're actually empty strings, use IF(p.magX = '', c.magX, p.magX) instead of the IFNULL test.
Related
I am trying to update "new" column value with new value but problem is my query remove previous data while inserting new value
What is want: here is example table structure,
Table name = agg_lvl primary key set = uid
uid | new
--------|--------
1 | 100
2 | 300
You can see "new" has 100 points, for example I send 100 new points to user 1, so new column value should be 100 + 100 = 200, right now with this code
$query4 = mysql_query("INSERT INTO agg_lvl (uid, new) VALUES ('$uid','$new')
ON DUPLICATE KEY UPDATE uid='$uid',new='$new'");
Not sure what
new = '$new'
I have tried both ways but no success = >
new = 'new + $new' or new = new + '$new'
You should make changes in your query
Make num = nun+$num to add new value to old one
Remove quotes arount $new because it is a number but not a string
Remove uid from set list because insert already point to that record
And your query should look so:
$query4 = mysql_query("INSERT INTO agg_lvl (uid, new) VALUES ('$uid','$new')
ON DUPLICATE KEY UPDATE new=new+$new");
Okay first i will answer with the proper way to do the same, In this case i am assuming that UID is unique, so you make a new table scorecard with UID as foreign key. Now rather than update, you just insert stuff to table like if UID 1 gains 10 and 20 points, there are two entries. onw with 10 and one with 20. Now to get his current points, you add all points where UID=1 .
Now in your implementation the correct query would be
UPDATE userData SET points = points + x WHERE UID = $uid
where x is the new points gained and points is the name of column
$query4 = mysql_query("INSERT INTO agg_lvl (uid, new) VALUES ('$uid','$new')
ON DUPLICATE KEY UPDATE uid='$uid',new=new+$new");
worked for me with help of #splash58
I have this id number in one column of my table,
0907003
0907004
0907005
1008005
1008006
1008007
1009001
1009002
1009003
When I add new value of 1009, it will add last three number of 004 because the last number begin with 1009 (10-09) is 003.
Or if I add new value of 1008, it will add last three number of 008
because the last number begin with 1008 (10-08) is 007.
Or if I add new value of 0907, it will add last three number of 006
because the last number begin with 0907 (09-07) is 007.
How to do this?
Many thanks in advance!
$front_id = $this->input->post('gameid'); //09
$middle_id = $this->input->post('netid'); //07
//$last_id will be generate automatically by sql
$forID = $front_id.$middle_id;
$sql = "INSERT INTO table ('colum') VALUES (".$forID.")"
You have to insert new id manually
$max_of_letsay1009 = mysql_result(mysql_query("select MAX(id) from table where id like '1009%'"),0);
// get the last 3 digits
$new_number = (int)substr($max_of_letsay1009,-3) + 1;
or you can try this too:
$new_id_of_letsay1009 = mysql_result(mysql_query("select MAX(id)+1 from table where id like '1009%'"),0);
this is just my idea, not yet tested and no error checking
You try this below query
If your value is 1009
SELECT MAX(RIGHT(ID,4))+1 FROM TableName WHERE LEFT(ID,4) = '1009'
It will return the max number of that series.
Try this query for dynamic ID length
SELECT MAX(RIGHT(ID,len(id)-LEN('1009')))+1 FROM #TEMP WHERE LEFT(ID,LEN('1009')) = '1009'
You can also use this query as sub query for the insert statement's ID column.
It is possible if it is not Auto_Increment coulmn.
Just need to write logic on insert time.
I have a table with values, primary keys are unique ids with increment.
Secondly I have another table that saves the ids from the first table along with other data. The ids are stored in a already_used column
the already_used column should look like this: 1,2,3,4,5,6....
Now I use a query with CONCAT to fill the already_used column with data:
UPDATE table2 SET already_used = CONCAT(already_used,', ". $id ."') WHERE id = $table_id
Now my problem is that if the already_used column is empty, concat will produce an output like this: ,1,2,3,4,5,6...
So there is a comma in front of the first value. How can I ajust the query so the first value doesn't actually get a comma in front of it?
Later I store the already_used data in a variable using PHP and then I want to find all rows from table1 that weren't already used using a NOT IN statement:
SELECT * FROM table1 WHERE id NOT IN($variable)
And if there's a comma in front it will give a mysql error.
Never store multiple values in a single column. Never!
Please change your DB desgin to store only one id per record in table2.
Use IF or CASE to test already_used before concatenating:
UPDATE table2
SET already_used = IF(already_used = '', '$id', CONCAT(already_used, ',$id'))
WHERE id = $table_id
In your second query, you should use FIND_IN_SET:
SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON FIND_IN_SET(t1.id, t2.already_used)
WHERE t2.id IS NULL
However, FIND_IN_SET can't be indexed. This is why a relation table is usually a much better design.
UPDATE table2 WHEN already_used IS NULL THEN SET already_used = CONCAT(already_used,'". $id ."') WHEN already_used IS NOT NULL THEN SET already_used = CONCAT(already_used,', ". $id ."') WHERE id = $table_id
I am inserting multiple rows using one query and, obviously, the ID column auto increments each row. I want to create another ID column and have the ID remain the same for all rows inserted during the query. So if I insert 10 rows during one query, I want all 10 rows to have the id "1". How can this be done? Thanks for any help
If I understood your question correctly, you want to supply an ID for the specific group of INSERT statements.
Assumming you have this schema
CREATE TABLE TableName
(
RecordID INT AUTO_INCREMENT PRIMARY KEY,
OtherColumn VARCHAR(25) NOT NULL,
GroupID INT NOT NULL
)
You can have two statements for this:
1.) Getting the last GroupID and increment it by 1.
SELECT COALESCE(MAX(GroupID), 0) + 1 AS newGroupID FROM TableName
2.) once you have executed it, store the value in a variable. Use this variable for all the insert statement,
$groupID = row['newGroupID'];
$insert1 = "INSERT INTO TableName(OtherColumn, GroupID) VALUES ('a', $groupID)";
$insert2 = "INSERT INTO TableName(OtherColumn, GroupID) VALUES ('b', $groupID)";
$insert3 = "INSERT INTO TableName(OtherColumn, GroupID) VALUES ('c', $groupID)";
UPDATE 1
SQLFiddle Demo
I want to store the data from several curl calls to an initial table, but last insert id is inserting wrong info
Query 1 inserts data into table
table1
id name email valuereturn
1 val val#email.com 0
I then post data 3 times to my system and log it
table2
id name system valuereturn
1 val 5 0
2 val 0 0
3 val 0 0
the max value returned from my system i want to update table 1
update table1
set valuereturn = '5'
where id = LAST_INSERT_ID()
does not work because last insert id is 3 from table2, how can I use something like last_insert_id(Table1)?
i want to update my
Well, you can't. You have to retrieve and remember it in a PHP variable.
Or go for Saharsh's solution and remember it in a MySQL variable.
Store that LAST_INSERT_ID() of table1 in a variable and than use that variable in update query.
INSERT INTO table1(name) values ('Saharsh');
SELECT LAST_INSERT_ID() INTO #table1Id;
INSERT INTO table2(name, table1id) values ('Saharsh', #table1Id);
UPDATE table1 SET valuereturn = '5' WHERE id = #table1Id;
In PHP I assume you use the following code:
<?php
connect_db();
insert_first_query_to_table1();
insert_second_query_to_table2();
update_query_setting(last_insert_id());
?>
If that's the case, I suggest you to use a temp variable to store the last_insert_id.
<?php
connect_db();
insert_first_query_to_table1();
$setVal = last_insert_id();
insert_second_query_to_table2();
update_query_setting($setVal);
?>
Hope this helps.
PS: This is a pseudo code!