e.g.
Table A (id - auto increment)
id labels names
1 a1,a3,b4 a1
2 a2,b5 a2
3 a1,b4 a2
What i want is to update names column such as "existing_names_data + ', B'" where labels like %b4%
I know following query works with integers not with strings, but i tried and failed anyways -
update TableA set names=names+ " B" where labels like '%b4%'
Is there any such query for strings?
Or, what should I do to get desired output?
Desired Output
id labels names
1 a1,a3,b4 a1, B
2 a2,b5 a2
3 a1,b4 a2, B
Thanks. Regards,
Use the CONCAT() function:
UPDATE foo SET bar = CONCAT(bar, ' B') WHERE foobar = 'barfoo';
..that said, you should really just use a different relational table and normalize your data rather than add comma/semicolon/space seperated columns.
Consider the following structure:
messages:
id
name
message_labels:
id
message_id (JOIN message_labels ON message_labels.message_id = message.id)
label
message_names:
id
message_id (JOIN message_names ON message_names.message_id = message.id)
name
For Strings you may use the || to concat strings:
update TableA set names=names ||' '|| 'B' where labels like '%b4%'
Related
This question already has an answer here:
Left join with default relation
(1 answer)
Closed 3 years ago.
I'm stuck on a MySQL problem involving alias lookup. I want a table/view/function/thing where I can look up aliases. I want it in SQL because I then want to use the output to match records for any of the aliases.
The problem is I want to implicitely include all the trivial cases where any IDs not in the table have an Alias of only themselves. That is, given an ID = 'E' not in the alias table, return an Alias of 'E'.
There are a small number of IDs with multiple Aliases, and a large number of IDs that are only identified by themselves.
A function would work if it could return a list of values, but that doesn't seem possible. It could return a string of delimited IDs, but I didn't see how to split the string and use the resulting IDs.
I can't get a view to work because I didn't see a way to pass in the target ID we want to look up Aliases for.
For example:
AliasTbl
ID Alias
-- -----
A A
A B
A C
B A
B B
B C
C A
C B
C C
RecordTbl
ID other fields
-- ------------ ...
A foo
B bar
C zot
D other
E another
...
So if ID = 'B' has aliases 'A', 'B', 'C', I want all three values returned. (Note that I am using the term "alias" loosely to include the ID itself.)
SELECT *
FROM RecordTbl
WHERE ID in
(SELECT Alias
FROM AliasTbl
WHERE ID = 'B');
...should return...
ID other fields
-- ------------ ...
A foo
B bar
C zot
For any value other than 'A', 'B' or 'C'...
SELECT *
FROM RecordTbl
WHERE ID in
( given any ID do some magic to return the ID as its only Alias );
So for ID = 'E', return...
ID other fields
-- ------------ ...
E another
A left join ensures that records not existing in Alias table return a null alias. Then ifnull() function replace null by record ID.
Select record.ID, ifnull(alias.Alias, record.ID)
from RecordTbl as record
left join AliasTbl as alias on record.D = alias.ID
how can i do something like this:
I have two tables, A and B. A --> B is one to many.
B has a varchar field that we will call field1.
I have also a sequence seq1 that i want to use in this way:
Suppose we have A1 and A2, two records belonging to table A. A1 has (B1,B2,B3,B4) and A2 has (B5,B6).
I want to use the sequence for each group and start from the beginning each time i change group to update field1 in B. So i will have somthing like
B1.field1 = 1, B2.field1 = 2, B3.field1 = 3, B4.field1 = 4
now the sequence start back from 1 for A2:
B5.field1 = 1, B6.field2 = 2.
Is there some complex nested structure to do that or i need a function?
I was thinking about using a temporary table ,playing a bit on indexes and sub countings but i don't manage to find a wayout.
Thanks
I have a table with 2 columns
NAME SYNONYM
----------------
A ALPHA
B ALPHA
B BITA
C GAMA
D DELTA
E BITA
I am looking for a SQL query that will check column SYNONYM. If it finds the same SYNONYM, for example it finds ALPHA in second row which is the same with row 1 it will change B and make it A. The change of B will take place everywhere in column NAME, not only in one row.
NAME SYNONYM
----------------
A ALPHA
A ALPHA
A BITA
C GAMA
D DELTA
A BITA
If it is difficult to alter the column NAME we could add a new column like this
NAME SYNONYM NEW
----------------------------
A ALPHA 1
B ALPHA 1
B BITA 1
C GAMA 2
D DELTA 3
E BITA 1
NOTE: This type of query is not possible in straight MySQL if its supposed to be a dynamic query... MySQL does not support recursive queries.. however if you know the column you would like to update then you can do this query in a roundabout way.
So if there are some columns you would like to update you can do it like this and it'll do exactly what you're asking for..
EDIT:
if you use user defined variables you could walk through the table like so:
SET #A := (SELECT DISTINCT name FROM example_table ORDER BY name LIMIT 0,1);
UPDATE example_table et,
(
SELECT
DISTINCT synonym
FROM example_table
WHERE name IN
(
SELECT
DISTINCT name
FROM example_table
WHERE synonym IN
(
SELECT
DISTINCT synonym
FROM example_table
WHERE name = #A
)
)
) t
SET et.name = #A WHERE et.synonym = t.synonym;
all you have to do is increment the 0 in the SET #A statement.. LIMIT 0,1... LIMIT 1,1.... that will give you a new distinct name each time that you can execute the update on. Hope that helps
I have MySQL and the query is:
select name,re_type from myTable
I want to replace all values of type:
1 = student
2 = teacher
So the result should be:
name re_type
---------------
Ron Student
Mac Teacher
Not like:
name re_type
---------------
Ron 1
Mac 2
Is it possible to make a query like that so I get the desired result in MySQL ?
You can use a CASE statement
SELECT name, CASE WHEN re_type = 1 THEN 'Student' WHEN re_type = 2 THEN 'Teacher' ELSE 'Donno' END AS re_type_text
FROM myTable
You can use a joint table that will store labels for your re_type like
re_type_id re_type_label
1 student
2 teacher
And alter your query by :
select t.name,l.re_type_label
from myTable t
inner join labelsTable l
on l.re_type_id = t.re_type
I think he wants to keep IDs on re_type field, and just decoding them when extracting.
You can use CASE WHEN or ELT(), MySql equivalent to Oracle's Decode(), like explained here
, but the Best Practice is to create an external table containing re_type and re_type_description fields, so if tomorrow you'll have new values, you don't have to change all your queries.
This has got to be a simple question, just brain-dumbing right now...
I've got one table, called 'foo'. It's got two columns, 'id' and 'username'.
The id is unique, but some of the usernames reference the same user; just one has a prefix on the username of 'xx_'.
ex:
ID USERNAME
1 bob
2 sam
3 xx_bob
How can I figure out which of the users have a counterpart with the 'xx_' prefix? And then which ones do not?
select * from foo where username
IN (select replace(username, 'xx_', '') from foo where username like 'xx_%')
What this does is compares the entire table against a sub list which is generated by the sub-query after the IN verb.
To do the opposite you can simply use a NOT IN in place of the IN.
NOTE: This is a t-sql (MS SQL 2005) query, should be similar in MySQL
This will give you the id's of both rows:
select * from foo a1 join foo a2 on (a2.username=concat('xx_',a1.username));
If you want every rows that isn't a duplicate, with the duplicate_id:
SELECT foo.*, f2.id AS duplicate_id FORM foo
LEFT OUTER JOIN foo AS f2 ON ( f2.username = concat( 'xx_', foo.username ) )
WHERE foo.id NOT LIKE 'xx_%'