Creating new column using keywords - mysql

I'm looking to create a new column with values that correspond with keywords in an existing column in mysql.
I'm trying to determine if a practice is private or public based on keywords in their establishment's name.
In plain english what i'm trying to get is a new column called Practice and insert value private if keywords are specialist, gleneagles.
Would appreciate any help on this matter.

If I understand what you're asking, you want to add a column to an existing table and then set that column's value (for each row) based on another column's value in that same row?
First, to create the column named Practice you can use MySQL's ALTER TABLE command:
ALTER TABLE YourTable ADD Practice VARCHAR(20);
I'm not sure what type you are aiming for here, so I'm just going with VARCHAR(20); you may also benefit from an enum based on the values you're using (or even a foreign-key to a lookup table).
After you've created your column, you can set its value:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn = 'specialist' THEN
'private'
WHEN SomeColumn = 'gleneagles' THEN
'private'
ELSE
'public'
END
If you don't want records that don't match to be set, simply drop the ELSE 'public' and they'll be defaulted to null.
UPDATE (to select with wildcards)
If you need to update rows with the related data being contained within larger text and you need wildcards, you can use MySQL's LIKE operator:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn LIKE '%specialist%' THEN
'private'
WHEN SomeColumn LIKE '%gleneagles%' THEN
'private'
ELSE
'public'
END
Alternatively, if you're only going to update to a single-value based on text containing multiple values, you can use a Regular Expression (via REXEXP) instead:
UPDATE YourTable
SET Practice = CASE
WHEN SomeColumn REGEXP 'specialist|gleneagles' THEN
'private'
ELSE
'public'
END

You are asking about denormalization, which means storing derived data. My initial reaction would be to not add another column, unless there were significant performance problems, instead I would create a view that gas the calculated value.
However, if you need the column, I would create a boolean column:
alter table mytable
add is_private boolean;
Then populate it like this:
update mytable
set is_private = (name like '%specialist%' or
name like '%gleneagles%')

Related

How to delete specific value from a column in SQL row?

I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.
You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.
You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.
If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?

How to update column if another column fulfill specific criteria?

I need to update one column if another column has a specific data.
Usually if I want to update one column, I do the following SQL Query:
UPDATE table1
SET field1 = replace(field1, 'oldstring', 'newstring')
But what I can't figure is how to make it look up one column, and if that field has some data, it should update another column's field.
Here is what I want to do.
look in table: phpbb_tree
under column: spouses_total
if the field is empty (has no data)
update column: page_template
update from: tree_body_spouse_1.html to: tree_body_single.html
So basically, I know how to do the "update" part, but don't know how to make it look first in one column, and if empty (or matches) it should do the following:
UPDATE phpbb_tree
SET page_template = replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
Hopefully someone could tell me how to write it up. I don't even know if it's even possible to do a search for an empty data in a column?
You could use CASE expression to fulfill different condition of replacement.
UPDATE phpbb_tree
SET page_template = (CASE
WHEN spouses_total is null
THEN replace(page_template, 'tree_body_spouse_1.html', 'tree_body_single.html')
ELSE page_template
END
);
Edit:
Please check this..
SQL Fiddle HERE

Trying to use update query to only update fields that are blank in Microsoft Access

I am trying to use an update query to update fields from one table to another for fields but only if the fields in the table that i am updating into is blank. If they contain information, I do not want to overwrite the existing data
e.g
Field: Name
Table: Table 1
Update to: [Table2.][Name]
Criteria:
I am unsure of what to put in the criteria. I tried, 'Is Null', Like "".
Here is an example:
UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Looking at the Query from within Access, you can switch to SQL view. You just need to put Is Null in the criteria column: UPDATE MyTable SET MyTable.FieldB = "MyNewValue" WHERE (((MyTable.FieldB) Is Null));
Furthermore, you can just write Is Null on alternate lines and it will count as OR.

Changing from an enum to a linked table

We are going to change something that we have an enum to an id of a linked table instead.
How would we go about that?
Our current enum name: strat
Our new linked name: stratid
What I was thinking was something along the lines of:
UPDATE table_name
SET stratid = (SELECT id FROM link_table WHERE stratname = table_name.strat);
I have not created the link table yet, right now it is all theory.
Will the above work?
Is there anything I should change in order to transfer from an enum to the linked table?
No gotchas for making the update other than that you need want to make triple sure that your link_table.ids are populated strictly in the order that table_name.strat options are defined.
For example, if strat is enum('FOO', 'BAR') then in linked_table the record with id == 1 should be the "FOO" record.
After that, you 'd perhaps want to make the stratid column NON NULL; this is not strictly equivalent to your previous arrangement, but it would probably be closer to what you want.
Yes, create the link table first,
set the stratname as unique,
use an auto increment ID
A lazy solution for insert link_table:
insert into link_table
select distinct strat from table_name order by strat;
However, I not sure is all the predefined enum is being used.
Also, without knowing size of the enum,
I can't suggest you do a manual insert.
If you look at enum ...
enum('...', '...', ...) <-- is just a comma separated value
So, here is the query to get the CSV :-
select column_type from information_schema.columns
where schema_name="table_name" and column_name = "strat";
You can combine with an programming language to do the link_table insertion.
Lastly, you UPDATE query is not very optimize, you can switch to use INNER JOIN.
But I assume is one-time job, so be it!

REPLACE statement with embedded IF() logic?

EDIT: This actually works fine, no idea why I thought otherwise.
I have a prices table which includes a column price_was which needs to contain the highest ever value for prices.
Is it possible to do a REPLACE query which would update this if required?
The following (which is simplified and built dynamically in PHP) doesn't seem to work.
REPLACE prices
SET price = 1.99,
price_was = IF(1.99 > price_was, 1.99, price_was)
id_product = 1
I'm thinking perhaps it's not possible, but would love to hear otherwise since I'm updating many records and need to be as efficient as possible.
The query you posted is indeed valid, try it for yourself. I would use an UPDATE though since you're only updating one field and the REPLACE can possible over-write other column data you want left alone.
Try INSERT ... ON DUPLICATE KEY UPDATE instead:
INSERT INTO prices (price, price_was, id_product)
VALUES (1.99, 1.99, 1)
ON DUPLICATE KEY UPDATE
price_was = IF(VALUES(price) > price_was, VALUES(price), price_was)
id_product = VALUES(id_product)
This will do either an INSERT or an UPDATE, while the REPLACE statement does either an INSERT or a DELETE followed by an INSERT. You are not able to reference old values in a REPLACE statement, probably because of the DELETE/INSERT semantics. From the docs:
Values for all columns are taken from
the values specified in the REPLACE
statement. Any missing columns are set
to their default values, just as
happens for INSERT. You cannot refer
to values from the current row and use
them in the new row. If you use an
assignment such as SET col_name =
col_name + 1, the reference to the
column name on the right hand side is
treated as DEFAULT(col_name), so the
assignment is equivalent to SET
col_name = DEFAULT(col_name) + 1.