Can I set a datafield value twice in a single mysql command - mysql

Suppose there is a data field education in my table profile, now I want to update education='01' where earlier education was 'BA' , similarly education='02' where education was 'MD'
So I can do this task like this
update profile set education='01' where education='BA';
update profile set education='02' where education='MD';
My question is can I do this task in one command only like
update profile set education='01' where education='BA' and set education='02' where education='MD';
This syntax is wrong, please tell me is this possible and how ?
If it is not possible, than also please let me know about it...

You can use a CASE statement in the SET clause, but be careful to include an ELSE case which sets the column to its current value -- otherwise, the rows that aren't matched by the two cases will be set to NULL.
UPDATE profile
SET education =
CASE
WHEN education = 'BA' THEN '01'
WHEN education = 'MD' THEN '02'
/* MUST include an ELSE case to set to current value,
otherwise the non-matching will be NULLed! */
ELSE education
END

Related

Allow or disallow value change in combo box based on initial value to new value

I am trying to write code for a data certification status combobox that will allow or not allow the user to change the status based on specific guidelines:
User may change status from Raw to Clean, no restriction
User may change status from Clean to Certified, no restriction
User may change status from Raw to Certified after answering yes to verify all necessary QA/QC has been done
User may change status from Clean to Raw, if answers yes to proceed and provides explanation in "Comments" field
User may not change status from Certified to Clean, or from Certified to Raw
I assume I would use a Select statement where Case 1 = Raw (ID=1), Case 2 = Clean (ID=2), Case 3 = Certified (ID=3). The IDs 1, 2, and 3 are the ID values in the lookup table for the data certification status'. What I'm struggling with is how I set the "Before" value to compare to the "Current" value, when the user has already selected a different value.
Also, I'm using the "OnChange" event. But wondering if "BeforeUpdate" or "OnDirty" events would be better.
Any help would be greatly appreciated.
use "BeforeUpdate" Event on combox
If Me.Combo3.OldValue = 1 And Me.Combo3.Value = 2 Then
Dim x
x = MsgBox("all necessary QA/QC has been done? ", vbYesNo)
If x = vbNo Then
MsgBox "any thing you want to write"
Cancel = True
End If
End If

Return single value based on conditional CASE

I have no background with MySQL and have not been able to find the appropriate stepping stones to accomplish writing this particular query.
Objective:
I have created a report that shows the approval status of artwork per page. Artists have to access the proof however to determine if there is any markup/notes on each page. I want to add single column to the report that will have one of three string values per page for each proof. The string will either be "Yes", No", or "Missed" based on certain conditions.
Each proof can have multiple pages. Each page can have multiple marks (think of drawing a circle around something). Each mark can have multiple notes. This is where, for me, things get hazy. I am aware of the need to avoid RBAR queries, but I am unsure how to query against multiple comments and then marks by page.
Conditions:
This is the RBAR query I have for handling the notes/comments. This is fine for dealing with each comment, but obviously on a report there is no reason to see each comment row.
SELECT *,
CASE WHEN note IS NULL OR ' ' AND deleted = FALSE
THEN 'Missed' ELSE 'Yes'
END AS 'Comment'
FROM rvm_comment
Knowing that a mark can have multiple comments, I need to determine if any rows in rvm_comment.note are NOT NULL or contain just a space, ' '. If so, the mark is represented as "Yes". Otherwise the mark would be "Missed".
This should then be condensed/nested in a manner that each mark is compared.
If any mark on a page is "Yes" then output in that row would be "Yes". Otherwise, "Missed".
If this doesn't make sense, hopefully the following information will shed some light.
Tables and Relationships:
DSE_OBJECT Table:
This table is associated with a request that a proof can be attached to. The ID is the PK.
RVM_REVIEW_OBJECT Table:
This table is essentially the proof instance itself. A proof can have any number of pages (min 1). ID is the PK, review_object is the FK.
RVM_MARK Table:
This table contains information on marks that are added to a proof, including the page they exist on. ID is the PK, review_object is the FK. (NOT LISTED) Deleted is used to prevent returning results where a mark is deleted by the user. rvm_mark.deleted = FALSE
RVM_COMMENT Table:
This table is where the notes (strings) are stored. Deleted is used to prevent returning results where a note is deleted by the user (rvm_comment.deleted = FALSE). There is a flaw in the system where a comment can be created but if the user doesn't hit "enter" on their keyboard after typing the text is not saved in to the DB. This is why we need to test for NULL in rvm_comment.note.
Raw Data for Testing and Summary: Dropbox with CSVs for the tables
As a courtesy I have included some raw data in CSV form for anyone will to try it out. (click Dropbox link above).
So, to summarize again. I am trying to write a query that will condense those tables to a single string (AS Comment),for each rvm_mark.page_no. The string is based on a) whether or not rvm_comment.note is NULL or ' ', and b) whether any rvm_mark.id that has matching rvm_mark.page_no, has rvm_comment.note that isn't NULL.
EDIT UPDATE 12/14/16:
Thanks to Barmar I was able to take a step towards the final result. I am currently stuck on trying to return the string 'No' for any situations where there are no comments. This should only be when rvm_review_object.id does not have a matching value in rvm_mark.review_object.
SELECT rvm_review_object.dse_object_id, rvm_review_object.id,
T_Mark.creator, T_Mark.review_object,
CASE WHEN T_Mark.review_object IS NULL THEN 'No'
ELSE T_Mark.Comment
END AS Comment
FROM rvm_review_object
LEFT JOIN(
SELECT rvm_mark.review_object, rvm_mark.creator, rvm_mark.id,
CASE WHEN MAX(T_Comment.Comment = 'Yes') = 1 THEN 'Yes'
ELSE 'Missed'
END AS Comment
FROM rvm_mark
LEFT JOIN(
SELECT rvm_comment.mark,
CASE WHEN MAX((rvm_comment.note IS NULL OR rvm_comment.note = '')
AND rvm_comment.deleted = FALSE) = 1
THEN 'Missed'
ELSE 'Yes'
END AS Comment
FROM rvm_comment
GROUP BY rvm_comment.mark) AS T_Comment
ON T_Comment.mark = rvm_mark.id
WHERE rvm_mark.deleted = FALSE
GROUP BY rvm_mark.review_object) AS T_Mark
ON T_Mark.review_object = rvm_review_object.id
WHERE rvm_review_object.creator != T_Mark.creator
You need to group the query by mark. You can then use MAX() to determine if any of the rows in the group match the condition.
SELECT mark,
CASE WHEN MAX((note IS NULL OR note = '') AND deleted = FALSE) = 1
THEN 'Missed'
ELSE 'Yes'
END AS Comment
FROM rvm_comment
GROUP BY mark

How to quickly assign an attribute value to all products in a category, in Magento

I would like to quickly update all products assigned to a particular category in Magento with a particular attribute/value. For example, all products in category Bed > Designer Bedding would have their google_product_category attribute value set to 2541. Anyone know how I could do this?
I'm not entirely sure what your WHERE clause would be based on your question but, you could use something similar to the following (and adjust as needed)
UPDATE [table] SET google_product_category = 2541 WHERE category = 'Bed'

Looking for changes between two identical mysql tables

I'm a bit struggeling to make my plan to work. I'm getting a csv export from a member administration system. The plan is to update this list to a wordpress database. In order to acomplish that, i have made two tables.
current_members AND new_members.
First I import the csv to new_members, i do a check to see if there are new members (compared with the current_members), and members that need to be deleted, if so, the member gets a new or a delete flag, (current_members.deleted = true & new_members.new = true. So far, so good. I just use this simple qyery:
UPDATE `new_memberSync` SET `new_memberSync`.`new` = '1' where `new_memberSync`.`regnr` NOT IN (select `regnr` from `current_memberSync`)
The second query to flag deleted:
UPDATE `current_memberSync` SET `current_memberSync`.`deleted` = '1' where `current_memberSync`.`regnr` NOT IN (SELECT `regnr` FROM `new_memberSync`)
Next, i want to preform a check on the content of all member's fields. If a member is updated, i want to set a updated flag. I have made a new query, its not complete yet, but i see a list of changed members. The quesstion, how do i set changed to 1 for all instances found with this query:
SELECT `new_memberSync`.`pe_code`,`new_memberSync`.`regnr`,`new_memberSync`.`voorletters`,`new_memberSync`.`roepnaam`,`new_memberSync`.`new`
FROM `new_memberSync`
JOIN `current_memberSync` ON `new_memberSync`.`regnr` = `current_memberSync`.`regnr`
WHERE `new_memberSync`.`roepnaam` <> `current_memberSync`.`roepnaam`
OR `new_memberSync`.`straatnaam` <> `current_memberSync`.`straatnaam`
OR ..
OR .. ETC.
I tried using something like : UPDATE new_membersSync SET changed = 1 ON ( HERE THE QUERY)

Dynamically selecting MySQL updates

This is more of a theory question. I have a page with a bunch of various textfields, dropdown boxes, etc. Each user has his/her "own page" that can be updated via this update page that I am referring to. He updates the fields at his choosing. It passes about 30 variables (if every field is inputted) to a "preview page". If the person likes the preview page, then they click an "update" button at the bottom of the preview page and all the various variables are updated into the appropriate MySQL table and their "own page" that others see is updated dynamically. (let me know if this explanation isn't clear).
Inserting this information for the first time is easy. However, when a user wants to update only a few of the fields for his page later, this is where I am confused. How do I make the MySQL update query dynamic to recognize an update to ONLY the fields on the page that the user wants to update (while he leaves the other fields blank, thus leaving the old information intact for those columns, and they are disregarded in the update query).
Let me know if what I'm asking doesn't make sense and I'll try again.
Thanks for your help.
The easiest way to do this would be
UPDATE MyTable m SET m.f1 = COALESCE(input1,m.f1), m.f2 = COALESCE(input2,m.f2), ....
WHERE m.id = key;
The COALESCE will return the first non-null value (or null if all values are null).
Note that you can insert a default value after the existing field value if you want to force a default.
Like so:
UPDATE MyTable m SET m.f1 = COALESCE(input1,m.f1,default1), m.f2 = COALESCE(input2,m.f2,default2), ....
WHERE m.id = key;
See: MySQL: how to use COALESCE
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce