set a column to copy a column from another table (on condition) - mysql

What I currently have is:
UPDATE card, records
IF(records.date_returned == null) THEN SET
card.last_seen = records.date_loaned
ELSE SET card.last_seen = records.date_returned
WHERE card.card_no = records.card_no
A little background-- the table records has two columns-- date_loaned and date_returned, with date_returned set as null by default. I was wondering whether its possible to change the last_seen column in temp_card to date_returned when it gets updated
Pretty sure its impossible, but I guess I'm trying my luck!
I was kinda hoping it to be automatic (e.g. when records get updated, this triggers the last_seen to change).

You can try like this..
Update Card A INNER JOIN Record B ON (A.card_no =B.card_no) SET A.last_seen =(
Case WHEN B.date_returned==null
then B.date_loaned
Else B.date_returned
End
)

How about using CASE like
UPDATE card, records
SET
card= CASE records.date_returned == null
THEN card.last_seen = records.date_loaned
ELSE SET card.last_seen = records.date_returned
WHERE card.card_no = records.card_no

Related

SQl Update with IF else statement

I want to update "status" column. I wanted to use UPDATE with IF condition something like this:
UPDATE rent_record
SET status = IF(status='pending', borrowed, IF(status='returnP', returned) )
WHERE WHERE ID='$name'
I am not sure if it'll be possible. Please suggest the best possible ways to do this.
Thank you
Your query is almost right, you need to add an else value for the second IF, which should probably be status i.e.:
UPDATE rent_record
SET status = IF(status='pending', 'borrowed', IF(status='returnP', 'returned', status))
WHERE ID='$name'
You should use CASE:
UPDATE rent_record
SET status = CASE WHEN status = 'pending' THEN 'borrowed'
WHEN status = 'returnP' THEN 'returned'
ELSE ...
END
WHERE ID=?

MySQL UPDATE query with exceptions

I have two tables: chapter and updates
I have an UPDATE query to set member ids from the updates table into the chapter table. The problem I am trying to find a syntax solution to is that I need to use one query, but have it NOT update (skip) the value if the update value is '0'. When an update is filed (to await UPDATE processing), not all ids are changed, and those that are not are saved into my updates table as '0' while valid changes are a seven digit integer. The problem arises when an UPDATE is applied, any existing ids are overwritten with the '0' when that field should actually have its existing value retained. A sample of my current query is:
UPDATE chapter
SET chapter.election_date = updates.election_date,
chapter.president = updates.president_id,
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE
updates.chapter_id = chapter.id
AND updates.installation_date < CURRENT_DATE ()
Based off of this example, I am trying to find a way to have chapter.president NOT be updated if updates.president_id = '0'
If this is doable, any help or guidance would be appreciated.
Just add this condition to the join clause:
UPDATE chapter
JOIN updates ON updates.chapter_id = chapter.id AND
updates.president_id != '0'
SET chapter.election_date = updates.election_date,
chapter.president = updates.president_id,
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE updates.installation_date < CURRENT_DATE ()
The below query will not update the chapter.president, if updates.president_id=0, but will update all other fields.
UPDATE chapter, updates
SET chapter.election_date = updates.election_date,
chapter.president = if(updates.president_id<>0,updates.president_id,chapter.president)
chapter.vice_president = updates.vice_president_id,
chapter.secretary = updates.secretary_id,
WHERE
updates.chapter_id = chapter.id
AND updates.installation_date < CURRENT_DATE()

MySQL ifnull sub query

I am trying to update a table and set a column, set_price, if it is null to a value from another table.
Both records share the same prod_id. Can someone look over my query and tell me whats wrong with it?
update list_items l
set purchased = "YES",
set_price = IFNULL(set_price,(select pricelast
from inventory i where i.prod_id=l.prod_id))
where l.list_id=1
A better way would be using join update with the use of case-when something as
update list_items l
left join inventory i on i.prod_id=l.prod_id
set l.purchased = 'YES',
l.set_price =
case
when l.set_price is null then i.pricelast else l.set_price
end

Copying One column from table to another table that has matching variables in another column

I hope I can explain this to make sense lol.
I am trying to copy variables from one hats_old.red to hats_new.red that match hats_new.name in both tables, if they do not match then i need it to do nothing so it does not null the value or set it to 0.
This is as far as ive gotten. This changes unmatched to 0 which i am trying to avoid and cannot figure the rest out.
This is for Mysql
Thank you
UPDATE hats_new
SET hats_new.red = (
SELECT hats_old.red
FROM hats_old
WHERE hats_old.name = hats_new.name LIMIT 1
);
An update with a join should do the trick:
UPDATE hats_new hn
JOIN hats_old ho ON hn.name = oh.name
SET hn.red = ho.red

MySQL Get "id" from select

I have a select statement:
SELECT id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Here's the output:
id content name
99708 10.6.252.41 server01.example.org
What I'd like to do is be able to get the id that is returned from the previous statement and USE the id as input into another statement (an UPDATE statement) that will increment the value of a single column in the same table.
An example UPDATE statement that I am wanting is:
update records SET hits = hits + 1 WHERE id = ID_FROM_SELECT;
Thanks in advance.
You can use user defined session variables for this if the SELECT is returning just one result:
SELECT #id:=id AS id, content, name
FROM records
WHERE type = '1'
AND name = 'test';
Then, on the same database session (connection), do the following:
UPDATE records
SET hits = hits + 1
WHERE id = #id;
I'm assuming you're doing something with the selected records in your app, and you're trying to save on performance by avoiding having to search for the record again in the UPDATE. Though, in that case, why not set the 'id' value as a parameter in code?
Obviously, if the SELECT is returning multiple records, this would best be done in code as I mentioned above, otherwise you're left with running the SELECT query again as a subquery:
UPDATE records
SET hits = hits + 1
WHERE id IN
(SELECT id
FROM records
WHERE type = '1'
AND name = 'test');
So, then, it makes more sense just to apply the same filter to the UPDATE instead:
UPDATE records
SET hits = hits + 1
WHERE type = '1'
AND name = 'test'
Probably this is not what you want to do.
First of all...If the query only returns 1 line, the solution provided by Marcus Adams works fine. But, if the query only returns one line, you dont need to preset the id in order to update. Just update it:
update records
set hits = hits + 1
where type = '1'
and name = 'test'
Second...If the query will not return only one record and you want to update all records returned with same values or calculations, the same code above will do what you need.
Third, if the query does not return just one record and you need to update each record returned with different value then you need to have a different approach.
I think you are not designing your system very well. If the request for update come from outside, you should have the id to be updated as a parameter of your request. For example something like:
<html>
<body>
Test
</body>
</html>
And in your update.php you have something like:
<?php
$id = $_GET['id'];
$sql = "update records set hits = hits + 1 where type = '1' and name = 'test' and id = $id";
?>
Of course, the picture I have is to small. Probably you have a reason to do this way or this is just an example. If you fill us up with more info we might be more helpful.